- by codepopular
- 17 Comments
- WordPress
- Views 1249
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 ?
- Create a folder in your plugin directory.
- Then Create a PHP file like demo.php
- Then write plugin header file
- Then Copy past above code in your file.
Do you have any query to us? Contact Us
Hi, My name is Shamim. I am a freelance PHP developer in Bangladesh. I have been working as a freelance developer since 2014. I am a passionate and creative web development person. As a senior level, I focus on your requirements in detail and deliver high-quality work on your budget.
Tags: WordPress
I love the efforts you have put in this, thank you for all the great articles. Kally Floyd Dorcy
Thank you very much.
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
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!
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
Thank you. Please contact with here to talk about this.
This is my first time visit at here and i am genuinely impressed to read all at one place.
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.
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.
Thank you. Please contact with here to talk about this.
Hi there mates, its enormous paragraph about educationand completely defined, keep it
up all the time.
Wow! After all I got a weblog from where I know how to in fact take useful information regarding my study and knowledge.
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!
I am really thankful to the owner of this site who has shared this impressive piece of writing at at this time.
Very shortly this web page will be famous amid all blogging and site-building viewers, due to it’s fastidious articles
I every time spent my half an hour to read this web site’s content everyday along with
a cup of coffee.
Saved as a favorite, I like your blog!