How to Save Plugin Information to the WordPress Database

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.

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.

Leave a Reply