WordPress Plugin Authoring Tutorial (iv): Saving data to a database

Source: Internet
Author: User
Tags autoload deprecated

The previous article explains how to add a menu, which explains how to save data to a database and display it on the page without the content in the Refresh page input box when the form is submitted. To achieve this function we need to use the WordPress function to implement, the following to explain the specific implementation method, first put the code out. or according to the previous plug-in sample, you can make changes directly on the above.

//Add a submenu in the WordPress background comment sectionAdd_action (' Admin_menu ', ' Comments_submenu ');functionComments_submenu () {add_comments_page (__ (' Data save '), __ (' Data save '), ' read ', ' My-unique-identifier-datasave ', ' Add_comments_submenu ');}//WordPress Back Office Comment Menu PagefunctionAdd_comments_submenu () {if($_post[' test_hidden '] = = ' Y ') {update_option (' Test_input_c ',$_post[' test_insert_options ']);//Update the database you added?> <div id= "message" style= "Background-color:green; Color: #ffffff; " > Save success!</div><?php}?> <div> <?php Screen_icon ();//Display icon?> for= "Test_insert_options" > Input test Data:</label> <input type= "text" id= "test_insert_options" Name= "Test_ins Ert_options "value=" <?php Echo esc_attr (get_option (' Test_input_c '));?> "/> class= "button Button-primary"/> <input type= "hidden" name= "Test_hidden" value= "y"/> </p&gt      ; </form> </div><?php}//Displays the information in the database by Get_option (). The above information is contained in the Wp_options table in the database. 

The short code above implements the function of saving and displaying data, which we can extend to other methods or functions. For example, some good themes with theme settings, some of the display and storage can be implemented using the above method, we can try a lot.

WordPress option Mechanism

See Creating Options pages, where you'll learn how to create a page that automatically saves option data.

WordPress has an "option" mechanism for saving, updating, and retrieving independent, specific names of data. The value of an option can be a string, an array, or even a PHP object (of course, the PHP object must be able to be serialized or converted to a string when it is saved, and must be deserialized when retrieved). The name of the option must be a string and must be unique in order to ensure that they do not conflict with woredpress or other plug-ins.

Typically, you'd better be able to streamline the number of plug-in options. For example, if you have 10 different name options that need to be saved to the database, then you can consider the 10 data as an array and save it to the same option in the database.

Here are the main functions that let your plugin use the option mechanism:

add_option ($name, $value, $deprecated, $autoload); //Official documents: http://codex.wordpress.org/Function_Reference/add_option//creates a new option and saves it to the database, and if the option already exists, no action is taken. $name the necessary parameters (string). The name of the option to create. $value an optional parameter (string). The value of the option to create. The default value is an empty string. $deprecated an optional parameter (string). Whether the option has expired. This parameter must pass in an empty string or null if you need to have the following $autoload parameter in effect. $autoload optional parameter (enum: ' Yes ' or ' no '). Whether the option is automatically loaded. If set to ' yes ', this option is automatically retrieved by the Get_alloptions function. The default value is ' Yes '.  delete_option ($option); //Official document: http://codex.wordpress.org/Function_Reference/delete_option//$option necessary parameters (string) Select the name of the option that you want to delete. A list of valid default options can be selected in the option reference reference.  update_option ($option _name, $newvalue); //Official document: http://codex.wordpress.org/Function_Reference/update_option//update the option values in the database and create this option if the option does not exist. (Note: If you do not use the $deprecated and $autoload parameters, you do not need the Add_option function). $option _name The necessary parameters (string). The name of the option to update/create. $newvalue necessary parameters (String|array|object). The value of the option to update/create.  get_option ($option); //Official document: http://codex.wordpress.org/Function_Reference/get_option//Gets the value of the specified option from the database. $option the necessary parameters (string). The name of the option. You can find a list of default options installed with WordPress in option Reference.
"Admin_email"--the email address of the blog administrator. "Blogname"--blog title, General Settings options. "Blogdescription"--For your blog banner; general settings options. For your blog "Blog_charset"--character encoding; set in Reading options. "Date_format"--Default date format, general settings options. "Default_category"--the default article category; Written setup options. "Home"--the URL of the blog, general settings options. In the "SiteURL"--wordpress website address, general settings options.    WARNING: This is not the same as Get_bloginfo (in the "SiteURL") (Will return the home page URL), but with Get_bloginfo ("Wpurl"). "Template"--the name of the current subject, expressed. "Start_of_week"--day Sunday calendar should start, general settings options. "Upload_path"--The default upload location, set in Miscellaneous options. "Posts_per_page"--the maximum number of posts displayed on the page, set in the reading options. "Posts_per_rss"--the maximum number of recent posts shows aggregated feeds; There can be more options, depending on what plug-in you have installed. Access/wp-admin/options. A complete list of PHP pages. Emphasize independent words, lowercase letters only-this will be in the database.

WordPress Plugin Authoring Tutorial (iv): Saving data to a database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.