Hello,
Do you already have a HTML form in a page? If so, all you need to do is intercept the POST request, like this:
add_action( 'template_redirect', function () {
if ( ! isset( $_POST['my_field'] ) ) {
return;
}
$value = $_POST['my_field'];
// process submitted data here
} );
Hello, thanks for the response!
This is my HTML form:
<form action=”???” method=”post”>
<input checked=”checked” name=”dye” type=”radio” value=”0″ /> Dye 1
<input name=”dye” type=”radio” value=”1″ />Dye 2
</form>
I’m not sure what to put in the “action” field.
Where do you have this form on your website? Is it in a page or a post or something?
In that case, I would recommend something similar to this:
add_action( 'template_redirect', function () {
if ( ! is_page( 42 ) || ! isset( $_POST['dye'] ) ) {
return;
}
$dye = $_POST['dye'];
// process submitted data here
} );
You should replace 42 above with the ID, slug or name of your page. You don’t need to fill in the action field on the form; the data will just be submitted to the current page.
When I try the code below I get:
“Undefined index: dye in /opt/bitnami/apps/wordpress/htdocs/wp-content/plugins/code-snippets/php/snippet-ops.php(352) : eval()’d code on line 2″
<form action=”” method=”post”>
<input checked=”checked” name=”dye” type=”radio” value=”0″ /> Dye 1
<input name=”dye” type=”radio” value=”1″ />Dye 2
</form>
add_action( ‘template_redirect’, function () {
$dye = $_POST[‘dye’];
print_r($dye);
} );
The check on lines 3-5 of the code snippet I posted is important – you are receiving this error because you removed it.