0

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 $_POST to 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.

2
  • what I frequently use is sending the form to admin-post.php like 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. Commented Jul 30 at 6:23
  • 2
    How did using $_POST not work? That's what you'll have to do, but only in the appropriate context - if the form was actually submitted. You could simply check, what $_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. Commented Jul 30 at 6:24

1 Answer 1

-1

Check this code. Try adding $_POST like this.

if (isset($_POST['ccf_submit']) && isset($_POST['ccf_nonce_field']) && wp_verify_nonce($_POST['ccf_nonce_field'], 'ccf_nonce_action')) {
        $name = sanitize_text_field($_POST['name']);
        $email = sanitize_email($_POST['email']);


        $output .= '<div class="ccf-success">Thank you, ' . esc_html($name) . '. We have received your email: ' . esc_html($email) . '.</div>';
    }

Here is the full code.

function custom_contact_form() {
    $output = '';
    $name = '';
    $email = '';


    if (isset($_POST['ccf_submit']) && isset($_POST['ccf_nonce_field']) && wp_verify_nonce($_POST['ccf_nonce_field'], 'ccf_nonce_action')) {
        $name = sanitize_text_field($_POST['name']);
        $email = sanitize_email($_POST['email']);


        $output .= '<div class="ccf-success">Thank you, ' . esc_html($name) . '. We have received your email: ' . esc_html($email) . '.</div>';
    }

    $output .= '
        <form method="post" action="' . esc_url($_SERVER['REQUEST_URI']) . '">
            ' . wp_nonce_field('ccf_nonce_action', 'ccf_nonce_field', true, false) . '
            <input type="text" name="name" value="' . esc_attr($name) . '" placeholder="Your Name" required>
            <input type="email" name="email" value="' . esc_attr($email) . '" placeholder="Your Email" required>
            <input type="submit" name="ccf_submit" value="Send">
        </form>
    ';

    return $output;
}
add_shortcode('contact_form', 'custom_contact_form');

Here I have added the form submission also inside custom plugin. And also added a validation part.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.