How to properly display form data using shortcode in a custom WordPress plugin?

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.