wordpress form data March 25, 2020

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

Tags:

Like this article? Spread the word

Share Share Share Share

Support Me to Buy a Coffee

If you like my blog or Free WordPress Theme and Plugin then Please make me happy to buy a Coffee to bing more inspired to contribute to it.

17 thoughts on “How to Save Plugin Information to the WordPress Database

  1. 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

  2. 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!

  3. 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

  4. 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.

  5. 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.

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

  7. 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!

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

Leave a Reply

Your email address will not be published.

Ask Question

    Support Me to Buy a Coffee

    If you like my blog or Free WordPress Theme and Plugin then Please make me happy to buy a Coffee to bing more inspired to contribute on it.