Ecommerce Shopify WordPress Discussion

How to process a form in WordPress and create an XML file out of its data?

I have a form, and out of the data that is submitted from this form I want to create an XML file and save this file to a specific path. I don't know how to pass the data to a PHP file in WordPress. Usually I would just pass the path of the PHP file in the action attribute and handle the data. But how do I do this in WordPress? I created the form without a plugin. I am new to WordPress and still trying to figure things out. This is the form: <form action="???" method="post"> <input type="hidden" name="action" value="XMLBanf"> <label for="name">Name:</label> <input type="text" name="name" required=""> <label for="departement">Abteilung</label> <select name="Abteilung"> <option value="keine_angabe">Keine Angabe</option> <option value="vertrieb">Vertrieb</option> <option value="it">IT</option> <option value="service">Service</option> <option value="lager">Lager</option> </select> <label for="order">Was soll bestellt werden: </label> <textarea name="order" cols="30" rows="10" required=""></textarea> <label for="reason_order">Grund der Bestellung</label> <input type="text" name="reason_order" required=""> <label for="price">Angebotspreis</label> <input type="number" name="price" required=""> <label for="offer_file"></label> <input type="file"> <input type="submit" name="submit_button" value="Abschicken"> </form> then I would try to handle the data like so: if(isset($_POST['submit'])){ $xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\" ?><order></order>"); $xml->addChild('name',$_POST['name']); $xml->addChild('departement',$_POST['departement']); $xml->addChild('order',$_POST['order']); $xml->addChild('reason_order',$_POST['reason_order']); $xml->addChild('price',$_POST['price']); $fileName = md5(uniqid(mt_rand(), true))."banf.xml"; $asXML= $xml->asXML(); $file = fopen($fileName, "w+"); fwrite($file, $asXML); fclose($file); print_r(error_get_last()); if(file_exists("./".$fileName)){ $myXML = file_get_contents("./".$fileName); $xml = new SimpleXMLElement($myXML); $xmlpretty = $xml->asXML(); // pretty print the XML in browser header('content-type: text/xml'); echo $xmlpretty; } How do I access this file now?
You can hook into the init action (https://developer.wordpress.org/reference/functions/add_action/) in your functions.php file or custom plugin. add_action( 'init', function() { if ( ! isset( $_POST['submit'] ) ) { return; } // Process submission here. } ); You can set the URL for the action of the form to the home_url() for example: action="<?php esc_url( home_url() ); ?>". Any WordPress generated page will trigger your init action, running your code. A couple of things to note: "submit" is too vague and can potentially conflict with other submits, so make sure you add a hidden input with a unique and clear name for your action. Make sure you use nonces for forms to combat CSRFs. While you might think that you don't have a vulnerability now, it's important to get used to adding nonces to all forms as a security best-practice: https://developer.wordpress.org/apis/security/nonces/#adding-a-nonce-to-a-form Don't forget to exit; and short-circuit your output to prevent WordPress from loading the homepage further.

December 30, 2023

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.