I'm creating a custom WordPress plugin that includes a contact form rendered using a shortcode.
Here's my current code:
function custom_contact_form() {
ob_start();
?>
<form method="post" action="">
<input type="text" name="name" required>
<input type="email" name="email" required>
<input type="submit" name="submit" value="Send">
</form>
<?php
return ob_get_clean();
}
add_shortcode('contact_form', 'custom_contact_form');
I tried:
- Using
$_POSTto capture form data inside the function - Adding
action="<?php echo esc_url($_SERVER['REQUEST_URI']); ?>"to the form - Checking if the shortcode runs correctly in a page
But I’m not sure how to process the form submission inside the shortcode, or how to show the submitted data.
admin-post.phplike in this code : wordpress.stackexchange.com/questions/428472/…. if you need more details about how to process the data, you can edit your question to add more details and add the code you tried.$_SERVER['REQUEST_METHOD']contains - but that could go wrong, if for some reason something else causes a POST request to that same page. Giving the submit button a more unique name, and then checking for that explicitly in $_POST, might be a bit safer.