wordpress form data

To create WordPress plugin first you need to setup header file in top of page. then you have to remind few thing to create a plugin in WordPress. you have to create a hidden input field to check submit form. then need to create nonce field to understanding correct user request. to submit WordPress form data two is generate method one is Option Api another is Settings Api

Validating the form
  <input type="hidden" name="must_submit" value="form_submitted">

Validating nonce field
 <?php wp_nonce_field('demo_nonce_check')?>
Create Plugin Admin Menu Page
// create custom plugin settings menu
add_action('admin_menu', 'codepopular_create_plugin_menu');
function codepopular_create_plugin_menu() {
    //create new top-level menu
    add_menu_page(
     'Demo Plugin Settings', //page title
     'Demo Plugin', //menu title
     'administrator', //capability
     'demo-plugin',  //page url
     'codepopular_plugin_settings_page' //callback 
   );
}
Handling form data with this code
//call register settings function
add_action('admin_init', 'register_codepopular_plugin_settings');
function register_codepopular_plugin_settings()
{
    if (isset($_POST['must_submit']) == 'form_submitted') {
        // nonce validation
        $nonce = $_POST['_wpnonce'];
        if (!wp_verify_nonce($nonce, 'demo_nonce_check')) {
            wp_die('are you cheating');
        }
        // user access validation
        if (!current_user_can('manage_options')) {
            wp_die('are you cheating');
        }
        // check if submit form
        $options['name']  = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
        $options['email'] = isset($_POST['email']) ? sanitize_text_field($_POST['email']) : '';
        $options['phone'] = isset($_POST['phone']) ? sanitize_text_field($_POST['phone']) : '';
        update_option('demo_testing_plugin', $options);
    }
}
Display input form
function codepopular_plugin_settings_page() {
    $options = get_option('demo_testing_plugin');
    $name    = ! empty($options['name']) ? $options['name'] : '';
    $email   = ! empty($options['email']) ? $options['email'] : '';
    $phone   = ! empty($options['phone']) ? $options['phone'] : '';
    ?>
<div class="wrap">
<h1>Your Plugin Name</h1>
<form method="post"action="">
    <table class="form-table">
        <tr valign="top">
        <th scope="row">Custom Name</th>
        <td>
          <input type="text" name="name" value="<?php echo esc_attr($name); ?>" />
        </td>
        <tr valign="top">
        <th scope="row">Customer Email</th>
        <td>
            <input type="email" name="email" value="<?php echo esc_attr($email); ?>" />
        </td>
        </tr>
        <tr valign="top">
        <th scope="row">Customer Phone</th>
        <td>
            <input type="text" name="phone" value="<?php echo esc_attr($phone); ?>" />
        </td>
        </tr>
    </table>
    <input type="hidden" name="must_submit" value="form_submitted">
    <?php wp_nonce_field('demo_nonce_check')?>
    <?php submit_button();?>
</form>
</div>
<?php }?>
How to setup this plugin in your WordPress ?
  1. Create a folder in your plugin directory.
  2. Then Create a PHP file like demo.php
  3. Then write plugin header file
  4. Then Copy past above code in your file.

Do you have any query to us?  Contact Us

17 comments

  • Author's gravatar
    bluray 12th January 2021 , 8:38 am

    I love the efforts you have put in this, thank you for all the great articles. Kally Floyd Dorcy

    Reply
  • Author's gravatar
    Trey 22nd February 2021 , 5:52 am

    First off I want to say superb blog! I had a quick question which I’d like to ask if you don’t mind.
    I was interested to know how you center yourself and clear your
    mind before writing. I have had a tough time clearing my thoughts in getting my thoughts out.

    I do take pleasure in writing however it just seems like the first 10 to 15 minutes are generally wasted simply just trying
    to figure out how to begin. Any suggestions or tips? Thank
    you!

    Visit my homepage: best CBD oil for dogs

    Reply
  • Author's gravatar
    Stephany 23rd February 2021 , 3:11 am

    I was recommended this blog by my cousin. I’m not sure
    whether this post is written by him as no one else know such detailed about my problem.
    You are wonderful! Thanks!

    Reply
  • Author's gravatar
    Marissa 23rd February 2021 , 3:48 am

    I really love your site.. Great colors & theme. Did you develop this web
    site yourself? Please reply back as I’m attempting to create my very own blog and
    want to find out where you got this from or just what the theme is named.
    Many thanks!

    Review my page – buy cbd gummies

    Reply
  • Author's gravatar
    Melaine 23rd February 2021 , 5:16 am

    This is my first time visit at here and i am genuinely impressed to read all at one place.

    Reply
  • Author's gravatar
    Armando 23rd February 2021 , 5:43 am

    Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something.
    I think that you can do with some pics to drive the message home a little bit, but other than that, this is magnificent blog.
    A fantastic read. I’ll definitely be back.

    Reply
  • Author's gravatar
    Chas 23rd February 2021 , 10:06 am

    Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would
    have some experience with something like this. Please let me know if you run into anything.
    I truly enjoy reading your blog and I look forward to your new updates.

    Reply
  • Author's gravatar
    Raymundo 23rd February 2021 , 12:51 pm

    Hi there mates, its enormous paragraph about educationand completely defined, keep it
    up all the time.

    Reply
  • Author's gravatar
    Katia 23rd February 2021 , 1:29 pm

    Wow! After all I got a weblog from where I know how to in fact take useful information regarding my study and knowledge.

    Reply
  • Author's gravatar
    Dario 24th February 2021 , 4:23 am

    That is a great tip especially to those new to the blogosphere.

    Simple but very accurate info… Appreciate your sharing this one.
    A must read article!

    Reply
  • Author's gravatar
    Dinah 24th February 2021 , 8:53 am

    I am really thankful to the owner of this site who has shared this impressive piece of writing at at this time.

    Reply
  • Author's gravatar
    Sadye 25th February 2021 , 5:16 am

    Very shortly this web page will be famous amid all blogging and site-building viewers, due to it’s fastidious articles

    Reply
  • Author's gravatar
    Senaida 25th February 2021 , 8:51 am

    I every time spent my half an hour to read this web site’s content everyday along with
    a cup of coffee.

    Reply
  • Author's gravatar
    Bobbye 25th February 2021 , 12:10 pm

    Saved as a favorite, I like your blog!

    Reply

Leave a Reply