Basic WordPress plugin Production tutorial, wordpress plugin Tutorial _php Tutorial

Source: Internet
Author: User

Basic WordPress plugin Production tutorial, wordpress plugin Tutorial


Preparation for plugin production

First we add a folder in the \wp-content\plugins directory called "My-mood", in the folder to add a main file called index.php, which is the main file of the plug-in, the beginning of the file requires some named format: the following code

 
  /*plugin name:my moodplugin uri:http://www.aips.medescription: A mood release plugin version:1.0author: 周良 Blog Author uri:http:// Www.aips.melicense:gpl*/?>
    • Plugin name represents the name of the plugin.
    • The Plugin URI represents the publishing address of the plug-in.
    • Description represents a description of the plugin.
    • Version is good, the first version uses 1.0, and if your plugin is updated, change this version parameter in turn.
    • Author represents the name of the plugin author.
    • The Author URI represents the author's home page:
    • License represents the plug-in License, if you are open source on the use of the GPL, about License parameters can be Baidu or Google query, there is no longer a lot of space to narrate.

Initialization of the plug-in installation

Plug-ins are more than just style changes, and usually we add new tables, and the new table is done with the plugin's installer, and we continue to add the following code to the index.php:

 
  Activate action Register_activation_hook (__file__, ' my_mood_install '), function My_mood_install () {//When enabled global $wpdb; table_name = $wpdb->prefix. "Mood"; $charset _collate = $wpdb->get_charset_collate (); $sql = "CREATE TABLE $table _name (ID mediumint (9) Not NULL AUTO _increment,createdon datetime default ' 0000-00-00 00:00:00 ' not null,publishedon datetime default ' 0000-00-00 00:00:00 ' N OT Null,status int not Null,mood int. not null,text text not null,address varchar (in) DEFAULT ' not null,unique KEY ID (ID )) $charset _collate; "; Require_once (Abspath. ' wp-admin/includes/upgrade.php ');d Bdelta ($sql);}? >

As noted in the code above, we use the Register_activation_hook activation action to complete the installation of the plug-in, the activation action through the parameter My_mood_install, find a function named My_mood_install execution, This action will be executed when the plugin is activated.

We created a table named "Mood" through the My_mood_install function, the database table is created by the WordPress Dbdelta function to execute the SQL statement, to use this function need to first introduce wp-admin/includes/ upgrade.php file.

Using the code above, we use the built-in method of WordPress to create a table that stores data for the mood plugin.

Plugin Uninstall

Since WordPress has installed will certainly have uninstall. The uninstall method of WordPress plugin is executed by a fixed named file called Uninstall.php, and a file named uninstall.php is built under the root of the plugin, the code content is as follows:

 
  Unload Action My_mood_uninstall (), function My_mood_uninstall () {//execute content Global $wpdb; $table _name = $wpdb->prefix. "Mood"; $wpdb->query ("DROP TABLE IF EXISTS". $table _name);}? >

Execute SQL via WordPress $wpdb->query, delete the table created at the time of our installation, and delete everything related to the plugin.

Add a background Management menu to a plugin

As in the following code:

 
  Add menu add_action (' Admin_menu ', ' my_mood_create_menu '), function My_mood_create_menu () {global $my _settings; $my _mood _settings=add_menu_page ("My Mood", "My Mood", "Manage_options", "My-mood", "Test");}? >

With the code above, we can add a menu to the plugin. method to add a menu through Add_action (' Admin_menu ', ' My_mood_create_menu ') and the menu specific page is bound by parameters, as the above method is passed the parameter called "Test", so when clicking on the "my Mood "menu will look for a method called" test "to output the style, we give the test method

 function test () {global $wpdb; $table _name = $wpdb->prefix. "Mood"; $fivesdrafts = $wpdb->get_results ("Select ID, CreatedOn, publishedon,status,mood,text,addressfrom $table _ Nameorder by CreatedOn DESC ");? >foreach ($fivesdrafts as $fivesdraft) {?>}?>
 
 
Publish Content now where the Mood Date Created Operation
Publish Content now where the Mood Date Created Operation
Happy:So so:Sad:Worries:Other: Add to
text;?> '/> address;?> '/> Happy:Mood==0? ' Checked=checked ': ';?> value= "0" > General:mood== ' 1 '? Checked=checked ': ';?> value= "1" > Sadness:Mood==2? ' Checked=checked ': ';?> value= "2" > worry:Mood==3? ' Checked=checked ': ';?> value= "3" > others:Mood==4? ' Checked=checked ': ';?> value= "4" > Save Delete
}?>

The test method is a form of PHP that is mixed with HTML code, where the HTML part is responsible for the output of the style, while the PHP code is responsible for executing the logic that takes the data. Mainly from the database to read the portion of the data, through the WordPress $wpdb->get_results method can be taken from the database in the table we created in the first step of the data, the return is a collection of data, contains multiple pieces of data. Finally, the data is output through a foreach loop.

We show the interface of the data, so how can we save the data? Also according to the previous mood plug-in example, first look at the following code

 
  function Aad_load_scripts ($hook) {global $my _settings;if ($hook! = $my _settings) return;/* loaded into the Ajax JS file, It can also be loaded into other JavaScript and/or CSS */wp_enqueue_script (' My-ajax ', Plugins_url (' My-mood/js/index.js ', __file), Array (' jquery ')); Wp_register_style (' My-style ', Plugins_url (' My-mood/css/style.css ', __file), Array (), ', ' all '); wp_ Enqueue_style (' My-style ');/* Create a validation nonce it will output a var aad_vars = {"aad_nonce":"5c18514d34"}; comment similar to: such as JS to HTML. */wp_localize_script (' My-js ', ' My_vars ', Array (' my_nonce ' = wp_create_nonce (' aad-nonce '))); Add_action (' admin_enqueue_scripts ', ' aad_load_scripts ');? >

Where the code for Index.js is as follows

jquery (document). Ready (function () {jQuery (' input '). blur (function () {var value=jquery (this). Val (); Jquery.ajax ({ Type: "POST", url: "/wp-admin/admin-ajax.php", DataType: ' JSON ', data:{action: "Say", Value:value},success:function ( Data) {}}); JQuery (". Add"). Click (function () {var parent=jquery (this). Closest ("tr"); var text=jquery (parent). Find ("Input[name=" Text '] "). Val (); var address=jquery (parent). Find (" Input[name= "). Val (); var mood=jquery (parent). Find (" Input[type= ' Radio ']:checked "). Val (); Jquery.ajax ({type:" POST ", url:"/wp-admin/admin-ajax.php ", DataType: ' JSON ', Data:{action: "Add_mood", text:text,address:address,mood:mood},success:function (data) {window.location.href= (window.location;});}) JQuery (". Delete"). Click (function () {var parent=jquery (this). Closest ("tr"); var id=jquery (parent). attr (' data '); Jquery.ajax ({type: "POST", url: "/wp-admin/admin-ajax.php", DataType: ' JSON ', data:{action: "Delete_mood", Id:id}, Success:function (data) {window.location.href=window.location;}});}) JQuery (". Edit"). Click (function () {VAr parent=jquery (this). Closest ("tr"), var id=jquery (parent), attr (' data '), Var text=jquery (parent). Find ("Input[name= ' Text ']. val (); var address=jquery (parent). Find ("Input[name="). Val (); var mood=jquery (parent). Find (" Input[type= ' Radio ']:checked "). Val (); Jquery.ajax ({type:" POST ", url:"/wp-admin/admin-ajax.php ", DataType: ' JSON ', Data:{action: "Edit_mood", id:id,text:text,address:address,mood:mood},success:function (data) { (window.location.href=window.location;});});

In the above code we use hooks to insert we need JS Code and CSS code, so our plugin's JS and CSS will be inserted into the page code because of the plug-in.
We implement asynchronous loading of the data according to the following code:

 
  function say () {$return =array (); $return [' success '] = ' 1 '; $return [' msg ']=$_post[' value ']. " Test-ajax "; Echo json_encode ($return);d ie ();} Add_action (' Wp_ajax_say ', ' say ');? >

This code means to use AJAX to submit data, the format of add_action (' wp_ajax_ function name ', functions name) is to register a say route, the corresponding JS code is

JQuery ("Input"). blur (function () {var value=jquery (this). Val (); Jquery.ajax ({type: "POST", url: "/wp-admin/ Admin-ajax.php ", DataType: ' JSON ', data:{action:" Say ", value:value},success:function (data) {}});})

So you can see that the JS code action is say

The same reason the data is to be added, register a add_mood route

 
  function Add_mood () {$text =$_post[' text ']; $address =$_post[' address '; $mood =$_post[' mood '];add ($text, $address, $ mood); $return =array (); $return [' success '] = ' 1 '; echo json_encode ($return);d ie ();} Add_action (' Wp_ajax_add_mood ', ' Add_mood ');? >

Data to be deleted, register a delete_mood route

 
  function Delete_mood () {$id =$_post[' id ');d elete ($id), $return =array (), $return [' success '] = ' 1 '; Echo Json_encode ($ return);d IE ();} Add_action (' Wp_ajax_delete_mood ', ' Delete_mood ');? >

To edit the data, register a edit_mood route

 
  function Edit_mood () {$id =$_post[' id '); $text =$_post[' text ']; $address =$_post[' address ']; $mood =$_post[' mood '];edit ($id, $text, $address, $mood); $return =array (); $return [' success '] = ' 1 '; echo json_encode ($return);d ie ();} Add_action (' Wp_ajax_edit_mood ', ' Edit_mood ');? >

The PHP functions corresponding to the above additions and deletions are shown below

 
  function Add ($text, $address, $mood) {global $wpdb; $table _name = $wpdb->prefix. "Mood"; $wpdb->insert ($table _name,array (' createdon ' = current_time (' mysql '), ' publishedon ' = Current_time (' MySQL '), ' status ' = 1, ' mood ' + $mood, ' text ' + $text, ' address ' + $address,); >
 
   function Delete ($id) {global $wpdb; $table _name = $wpdb->prefix. "Mood"; $wpdb->delete ($table _name,array (' id ' = $id)}? >
 
   function edit ($id, $text, $address, $mood) {global $wpdb; $table _name = $wpdb->prefix. "Mood"; $wpdb->update ($table _name,array (' mood ' = $mood, ' text ' = = $text, ' address ' + $address,), Array (' ID ' = $id)}? >

Now plug-in background data and interface have been processed, then how to put our mood plug-in in the foreground reference it? We need to add the following code

 
  function Mood_dispaly () {global $wpdb; $table _name = $wpdb->prefix. "Mood"; $fivesdrafts = $wpdb->get_results ("Select Textfrom $table _nameorder by CreatedOn desclimit 10");? >
 
   }?>

This code will be stored in the database of the mood data through the HTML display in the foreground, then where to control the appearance? Remember the first step we added JS and CSS, yes, style is the first step into the style to control.

To this a complete mood plug-in will be completed, according to the example you can make a part of their own mood plug-in.

http://www.bkjia.com/PHPjc/1075077.html www.bkjia.com true http://www.bkjia.com/PHPjc/1075077.html techarticle Basic wordpress Plugin production tutorial, wordpress plugin Tutorial Plugin Preparation Work First we add a folder under the \wp-content\plugins directory called "My-mood" in the folder ...

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