Storing data in wordpress
The task of saving data is worth it every time you need to program something., and when developing plugins, this is one of the first tasks after creating the interface part. But data in WordPress can be stored in different ways. I offer two options.
- Saving data with the add_option function for options.
- Adding data to a new database table to store large amounts of data and those that can change all the time.
Consider how to properly store options.
Place the code in the plugins folder in a new file named add-my-options.php.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<?php /** * @package Package name * @version 1.0 */ /* Plugin Name: My plugin add-my-options Plugin URI: https://wp-admin.com.ua Description: спасибо сайту wp-admin.com.ua, он научит меня делать плагины ) Armstrong: My Plugin. Author: Name Author Version: 1.0 Author URI: https://wp-admin.com.ua */ add_action('admin_menu', 'create_custom_panel'); function create_custom_panel() { add_menu_page('menu page', 'Add data', 'manage_options', 'custom-panel', 'custom_panel'); } function custom_panel(){ echo '<div class="wrap"> <h2>Сохраняем опции плагина</h2> <form method="post" action="options.php"> '.wp_nonce_field('update-options').' <table class="form-table"> <tr valign="top"> <th scope="row">Опция 1</th> <td><input type="text" name="my_option_first" value="'.get_option('my_option_first').'" /></td> </tr> <tr valign="top"> <th scope="row">Опция 2</th> <td><input type="text" name="my_option_second" value="'.get_option('my_option_second').'" /></td> </tr> <tr valign="top"> <th scope="row">Опция 3</th> <td><input type="text" name="my_option_third" value="'.get_option('my_option_third').'" /></td> </tr> </table> <input type="hidden" name="action" value="update" /> <input type="hidden" name="page_options" value="my_option_first,my_option_second,my_option_third" /> <p class="submit"> <input type="submit" class="button-primary" value="Сохранить" /> </p> </form> </div>'; } ?> |
After placing the code, activate the plugin in the admin panel and if everything is done correctly, you will see the item Add data in the admin menu on the left.
Consider the code of the written plugin in order
Function wp_nonce_field(‘update-options’) adds two hidden fields that allow you to automatically check the user's rights to change settings and redirect him back.
Function get_option() displays the saved data in the input field. Another piece of code located at the end of the form is responsible for writing options from the input fields.
1 2 |
<input type="hidden" name="action" value="update" /> <input type="hidden" name="page_options" value="my_option_first,my_option_second,my_option_third" /> |
«page_options» contains a comma-separated list of all settings, to be recorded when saving.
On the official site WordPress found another example of data storage. It seems to me, it will be more correct as there is an explicit separation of form template and code. Code looks much cleaner. I won't describe everything, everything is clear from the code, if you don't understand write in the comments, I will try to explain.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<?php // create custom plugin settings menu add_action('admin_menu', 'baw_create_menu'); function baw_create_menu() { //create new top-level menu add_menu_page('BAW Plugin Settings', 'BAW Settings', 'administrator', __FILE__, 'baw_settings_page',plugins_url('/images/icon.png', __FILE__)); //call register settings function add_action( 'admin_init', 'register_mysettings' ); } function register_mysettings() { //register our settings register_setting( 'baw-settings-group', 'new_option_name' ); register_setting( 'baw-settings-group', 'some_other_option' ); register_setting( 'baw-settings-group', 'option_etc' ); } function baw_settings_page() { ?> <div class="wrap"> <h2>Your Plugin Name</h2> <form method="post" action="options.php"> <?php settings_fields( 'baw-settings-group' ); ?> <table class="form-table"> <tr valign="top"> <th scope="row">New Option Name</th> <td><input type="text" name="new_option_name" value="<?php echo get_option('new_option_name'); ?>" /></td> </tr> <tr valign="top"> <th scope="row">Some Other Option</th> <td><input type="text" name="some_other_option" value="<?php echo get_option('some_other_option'); ?>" /></td> </tr> <tr valign="top"> <th scope="row">Options, Etc.</th> <td><input type="text" name="option_etc" value="<?php echo get_option('option_etc'); ?>" /></td> </tr> </table> <p class="submit"> <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" /> </p> </form> </div> <?php } ?> |
I did not have time to check the last example, but the first one works stably. I will try to describe working with databases in the next issues..
Good luck with plugin development!
/*
- Basic web design course;
- Site layout;
- General course on CMS WordPress and continuation of the course on template development;
- Website development in PHP.
Another article in addition to working with options.
http://wp-admin.com.ua/rassmotrim-mehanizmyi-rabotyi-s-optsiyami/