Joke Collection Website - Blessing messages - How to develop a WordPress plug-in
How to develop a WordPress plug-in
I. Give the plug-in a personalized name. The more personalized the better, in case of conflict with other plug-ins.
Two. In the code, you must include comments, not for yourself, but also for others who want to modify your code.
Three. Try to use the latest version of WordPress for development and testing.
Plug-in name and plug-in structure
Generally speaking, create a folder in the wp-content\plugins directory, and the folder name is the name of the plug-in. The name of our plug-in is "copyright_plugin" and the file structure is as follows.
\ WP-content \ plugins \ copyright _ plugin has copyright_plugin.php and readme.txt
The readme.txt file here contains some information such as the introduction and instructions of the plug-in, which is mainly used when it is submitted to WordPress official website. You can refer to the template they gave you, as well as the preview function of plug-in screenshots. You need supplementary pictures, but we are practicing now, so we don't need to submit them to official website, so we won't cover these for the time being.
The core of plug-in
Generally speaking, the core of plug-ins is two functions, which are used to add hooks.
add_action ($hookname,$callbackfunction)
add_filter ($hookname,$callbackfunction)
These two methods are so important that almost all plug-ins use them.
Actions, as I understand it, are some special entry points reserved by wordpress core code, or occur when certain events are executed, such as article publishing or guest messages.
Filters should be the filtering mechanism of data transmission during wordpress execution, such as the process of saving articles to the database, or the process of taking articles out of the database and presenting them to the browser.
The plug-in we are going to do today should use filters, because before the article is displayed in the browser, we need to add a character psychic at the end of the article to display copyright information, and we also need to use actions when adding menu items later.
Plug-in profile information
Open the copyright-plugin.php file with a text editor and enter the following information:
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
/*
Plug-in Name: Copyright Plug-in
Plug-in URI:
Description: This plugin will display a line of copyright information at the bottom of the text.
Version: 1.0.0
Author: xcxc
Author URI:
License: GPL
*/
& gt
Save the file, then log in to the WordPress background and open the plugin menu. You should be able to see this plug-in, which has been displayed in the plug-in list. You can enable this plug-in to try, but it has no effect, because so far, this plug-in has not achieved any function.
Realize the function of plug-in
Modify copyright _ plug-in. php
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
/*
Plug-in Name: Copyright Plug-in
Plug-in URI:
Description: This plugin will display a line of copyright information at the bottom of the text.
Version: 1.0.0
Author: xcxc
Author URI:
License: GPL
*/
/* This plugin will display a line of copyright information at the bottom of the article body */
Function display_copyright() {
return " & ltp style = ' color:red ' & gt; All articles on this site are original, please indicate the source! & lt/p >; ;
}
& gt
Save this file, then open the theme folder in use, open the index.php folder and find get _ template _ part ('content', get _ post _ format());); And add the following code below.
if(function _ exists(' display _ copyright '){
Echo Display _ Copyright ();
}
Function_exists is to judge whether the display_copyright function exists, because this function cannot be found in the theme code when the plug-in is disabled, so judge it to prevent errors.
Then enable the plug-in in wordpress background, and then open the home page to see the effect!
At this point, this code should be barely a plug-in. Helpless, because there is something wrong with this plug-in, you need to manually modify the theme code. If the user changes the theme, you need to modify the code again in the new theme, which is not good.
Improved plug-in
Remember the hook we talked about before? We're going to start using filter hooks!
Code modification copyright_plugin.php is as follows:
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
/*
Plug-in Name: Copyright Plug-in
Plug-in URI:
Description: This plugin will display a line of copyright information at the bottom of the text.
Version: 1.0.0
Author: xcxc
Author URI:
License: GPL
*/
add_filter( 'the_content ',' display _ copyright ');
/* This function adds a piece of copyright information at the end of the log text, and only adds */
Function display _ copyright ($ content) {
if( is_home())
$content = $content。 “& ltp style = ' color:red ' & gt; All articles on this site are original, please indicate the source! & lt/p >; ;
Return to $ content
}
& gt
Look at the code above, where _content is the name of the hook and display_copyright is the name of the callback function. In this way, as long as the plug-in is enabled, the function can be realized without modifying the theme. Please delete the code just added to the index.php file, and then enable the plug-in to see the effect. The display effect should be the same. No matter how you change the theme, the copyright information will be displayed automatically.
Is_home is to determine whether it is a home page.
ulteriorly
At this point, a real plug-in is completed. But this plug-in directly writes the copyright information in the code. If users want to customize copyright information, it is still not convenient to modify the source code of the plug-in, so this plug-in still needs to be improved. In fact, we can add separate menus and pages for plug-ins in WordPress background, where users can customize and set copyright information, and the information can be saved in the database.
Attach the complete code first, which will be explained later.
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
/*
Plug-in Name: Copyright Plug-in
Plug-in URI:
Description: This plugin will display a line of copyright information at the bottom of the text.
Version: 1.0.0
Author: xcxc
Author URI:
License: GPL
*/
/* Register the function to be called when the plug-in is activated */
register _ activation _ hook(_ _ FILE _ _,' display _ copyright _ install ');
/* Function to be called when registering to deactivate the plug-in */
register _ deactivation _ hook(_ _ FILE _ _,' display _ copyright _ remove ');
The function display_copyright_install ()
/* Add a record in the wp_options table of the database, and the second parameter is the default value */
add _ option(" display _ copyright _ text "," & ltp style = ' color:red ' & gt; All articles on this site are original, please indicate the source! & lt/p >; ,'',' Yes');
}
Function display_copyright_remove() {
/* Delete the corresponding record in wp_options table */
delete _ option(' display _ copyright _ text ');
}
if( is_admin() ) {
/* Use the admin_menu hook to add a menu */
add_action('admin_menu ',' display _ copyright _ menu ');
}
Function display_copyright_menu() {
/* add_options_page( $page_title,$menu_title,$capability,$menu_slug,$ function); */
/* Page name, menu name, access level, menu alias, callback function when clicking menu (display setting page) */
Add_options_page ('Set Copyright',' Copyright Menu',' Administrator',' Display _ Copyright',' Display _ Copyright _ html _ Page');
}
Function display_copyright_html_page() {
& gt
& ltdiv & gt
& lth2 & gt set copyright & lt/H2 >
& ltform method = " post " action = " options . PHP " & gt;
& lt? Php /* The following line of code is used to save the form contents to the database */? & gt
& lt? PHP WP _ nonce _ field(' update-options '); ? & gt
& ltp & gt
& lt text area
name="display_copyright_text "
id = " display _ copy _ text "
cols="40 "
rows="6 " >& lt? PHP echo get _ option(' display _ copyright _ text '); ? & gt& lt/textarea & gt;
& lt/p & gt;
& ltp & gt
& ltinput type = " hidden " name = " action " value = " update "/& gt;
& ltinput type = " hidden " name = " page _ options " value = " display _ copyright _ text "/& gt;
& ltinput type = " submit " value = " Save " class = " button-primary "/& gt;
& lt/p & gt;
& lt/form & gt;
& lt/div & gt;
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
}
add_filter( 'the_content ',' display _ copyright ');
/* This function adds a piece of copyright information at the end of the log text, and only adds */
Function display _ copyright ($ content) {
if( is_home())
$content = $content。 get _ option(' display _ copyright _ text ');
Return to $ content
}
& gt
Description:
The following code itself is called when the plug-in is enabled and disabled, which has been written in detail in the comments.
/* Register the function to be called when the plug-in is activated */
register _ activation _ hook(_ _ FILE _ _,' display _ copyright _ install ');
/* Function to be called when registering to deactivate the plug-in */
register _ deactivation _ hook(_ _ FILE _ _,' display _ copyright _ remove ');
The function display_copyright_install ()
/* Add a record in the wp_options table of the database, and the second parameter is the default value */
add _ option(" display _ copyright _ text "," & ltp style = ' color:red ' & gt; All articles on this site are original, please indicate the source! & lt/p >; ,'',' Yes');
}
Function display_copyright_remove() {
/* Delete the corresponding record in wp_options table */
delete _ option(' display _ copyright _ text ');
}
The code for adding menus and pages is as follows:
if( is_admin() ) {
/* Use the admin_menu hook to add a menu */
add_action('admin_menu ',' display _ copyright _ menu ');
}
Function display_copyright_menu() {
/* add_options_page( $page_title,$menu_title,$capability,$menu_slug,$ function); */
/* Page name, menu name, access level, menu alias, callback function when clicking menu (display setting page) */
Add_options_page ('Set Copyright',' Copyright Menu',' Administrator',' Display _ Copyright',' Display _ Copyright _ html _ Page');
}
At this point, you can already see the menu item in the background, but if you click it, you will get an error, because the corresponding page has not been added (how to customize the location of adding the background menu).
Let's start adding pages. The main codes are as follows:
Function display_copyright_html_page() {
& gt
& ltdiv & gt
& lth2 & gt set copyright & lt/H2 >
& ltform method = " post " action = " options . PHP " & gt;
& lt? Php /* The following line of code is used to save the form contents to the database */? & gt
& lt? PHP WP _ nonce _ field(' update-options '); ? & gt
& ltp & gt
& lt text area
name="display_copyright_text "
id = " display _ copy _ text "
cols="40 "
rows="6 " >& lt? PHP echo get _ option(' display _ copyright _ text '); ? & gt& lt/textarea & gt;
& lt/p & gt;
& ltp & gt
& ltinput type = " hidden " name = " action " value = " update "/& gt;
& ltinput type = " hidden " name = " page _ options " value = " display _ copyright _ text "/& gt;
& ltinput type = " submit " value = " Save " class = " button-primary "/& gt;
& lt/p & gt;
& lt/form & gt;
& lt/div & gt;
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
}
Now, click the newly added menu to display a page where we can set the copyright information text.
There is still the following code to be modified.
/* This function adds a piece of copyright information at the end of the log text, and only adds */
Function display _ copyright ($ content) {
if( is_home())
$content = $content。 get _ option(' display _ copyright _ text ');
Return to $ content
}
The original static text has become dynamic text. The reason for this change is that we have saved the data in the database, and here we want to take the data out of the database.
- Related articles
- How to write a composition at the beginning and end of Unit 4 of Volume 1 for Grade 6
- Apple's standby time is very power-consuming. How to solve it?
- How to do mass mailing?
- What is the charge for prepaid users roaming in Egypt?
- Is it okay to send a notice on WeChat when you are forced to leave your job?
- A warm word before going to bed, good night
- Tree planting activity plan
- Say hello to my brother.
- Are Everbright Xinglong Trust Products Reliable?
- How to turn off Huawei's 4x mobile phone and answer the vibration prompt?