Analysis on WordPress (WP) Plug-in mechanism

Source: Internet
Author: User

WordPress uses the plug-in mechanism to provide great scalability for a basic CMS-based Blog system.

First, the interface between the WP plug-in and the file system is the WP-content/plugins folder. The main interface file can be directly placed in this folder. If there are many files involved, you can create another folder and put it in this folder, however, this folder should be the same as the main file name of the interface: for example, the main interface file is myplugin. PHP, the folder name should be myplugin.

The plug-in can be divided into two parts: Comments in front and code area in comments.

The comments are roughly as follows:

/*
Plugin name: mycopyright
Plugin URI: http://www.why100000.com
Version: 0.1
Author: Mesh
Author URI: http://blog.why100000.com
Description: Set the string "& lt ;! -- Mycopyrigth -- & gt; "is replaced with the copyright information: Show copyright once there are letters match" & lt ;! -- Mycopyri.pdf -- & gt ;".
You shoshould config your copyright information in this file, with this verison.
*/

Do not simply look at the above comments. They are not casually written but in a fixed format. The information such as "plugin name" in front of the colon is useful. As the plug-in information, it will appear in the system settings and other menus as the ID information of this plug-in. The information after the colon can be rewritten as needed. A row connected by a colon is similar to a record in a table and cannot be wrapped in a line break.

For example, wp2.3.3 has a plug-in with the following annotations:

/*
Plugin name: Hello Dolly
Plugin URI: http://wordpress.org /#
Description: This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words using most famously by Louis Armstrong: Hello, Dolly. when activated you will randomly see a lyric from <cite> Hello, Dolly </CITE> In the upper right of your admin screen on every page.
Author: Matt mullenweg
Version: 1.5
Author URI: http://photomatt.net/
*/

Second, the code is complicated. However, there should be a similar

Add_action ('admin _ footer ', 'Hello _ dolil ');

PHP code, that is, at least the add_action function must be executed. When WP executes the admin_footer function, it executes the custom hello_dolly function. The hello_dolly function writes the logic of the special plug-in.

You can see the plug-in hello. PHP is very simple. The function is: When you click the menu item managed by the WP background, in the upper-right corner of the page, there are some "slogans" similar to "Chairman Mao quotations" (the description of the plug-in accurately describes its functions ). The add_action function is used in the plug-in:

Add_action ('admin _ footer ', 'Hello _ dolil ');
Add_action ('admin _ head', 'dolly _ CSS ');

The custom functions hello_dolly and dolly_css are executed respectively. Display Information and define CSS styles respectively.

Third, I imitated the example on the network and wrote a simplified WP plug-in. All the code is as follows:

<? PHP
/*
Plugin name: mycopyright
Plugin URI: http://www.why100000.com
Version: 0.1
Author: Mesh
Author URI: http://blog.why100000.com
Description: Set the string "& lt ;! -- Mycopyrigth -- & gt; "is replaced with the copyright information: Show copyright once there are letters match" & lt ;! -- Mycopyri.pdf -- & gt ;".
You shoshould config your copyright information in this file, with this verison.
*/

// When WP executes the the_content function, it calls the whins custom function why100000_showcopyright to filter the article content.
Add_filter ('the _ content', 'why100000 _ showcopyright ');
Function why100000_showcopyright ($ content)
{
$ Search = "<! -- Mycopyright --> ";
$ Replace = "<font color = 'red'> This article is original by mesh. For more information, see <a href = 'HTTP: // blog.why00000.com 'target =' _ blank '> Original Author: mesh (Zhang Qing) </a> </font> ";
$ Content = str_replace ($ search, $ replace, $ content );
Return $ content;
}
?>

Function: Set the string "<! -- Mycopyri.pdf --> "is replaced with the copyright information string. Add_filter is actually a callback function used to process some transactions when executing the the_content function of WP. For more information, see define the why100000_showcopyright function.

In this simple example, some additional functions can be added to make it more powerful. This article comes with an example that has extended some features. For more information, see.
Download: http://blog.why100000.com/Uploads/rar/myCopyright.rar

This is only the knowledge of the application layer of the WP plug-in. To learn more about the WP plug-in mechanism, read the source code of WP. Fortunately, it is open-source and more experience and skills should be learned from it.

Author: Zhang Qing (mesh) 2009-4-1
From mesh horizon: http://blog.why100000.com
Why 100,000 computer learning networks: http://www.why100000.com

Appendix: source code of the hello. php plug-in. Save it to the plugin folder and use it normally in wp2.3.3:

<? Php <br/>/* <br/> plugin name: Hello Dolly <br/> plugin URI: http://wordpress.org/# <br/> Description: This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words into most famously by Louis Armstrong: Hello, Dolly. when activated you will randomly see a lyric from <cite> Hello, Dolly </CITE> In the upper right of your admin screen on every page. <br/> Uthor: Matt mullenweg <br/> Version: 1.5 <br/> author URI: http://photomatt.net/<br/> */</P> <p> // These are the lyrics to Hello Dolly <br/> $ lyrics = "hello, dolly <br/> Well, hello, Dolly <br/> it's so nice to have you back where you belong <br/> You're lookin' pretty, dolly <br/> I can tell, Dolly <br/> you're still glowin ', you're still crowin' <br/> You're still goin' strong <br/> we feel the room swayin '<Br/> while the band's playin' <br/> one of your old favorite songs from way back when <br/> SO, take her wrap, fellas <br/> Find her an empty lap, fellas <br/> dolil'll never go away again <br/> Hello, Dolly <br/> Well, hello, dolly <br/> it's so nice to have you back where you belong <br/> You're lookin' pretty, Dolly <br/> I can tell, dolly <br/> You're still glowin', You're still crowin' <br/> You're st Ill goin 'strong <br/> we feel the room swayin' <br/> while the band's playin' <br/> one of your old favorite songs from way back when <br/> Golly, gee, fellas <br/> Find her a vacant knee, fellas <br/> dolil'll never go away <br/> dolil' ll never go away again "; </P> <p> // here we split it into lines <br/> $ lyrics = explode ("/N", $ lyrics ); <br/> // and then randomly choose a line <Br/> $ chosen = wptexturize ($ lyrics [mt_rand (0, count ($ lyrics)-1)]); </P> <p> // This just echoes the chosen line, we'll position it later <br/> function hello_dolly () {<br/> global $ chosen; <br/> echo "<p id = 'dolil'> $ chosen </P> "; <br/>}</P> <p> // now we set that function up to execute when the admin_footer action is called <br/> add_action ('admin _ footer ', 'Hello _ dolil'); </P> <p> // we need some CSS t O position the paragraph <br/> function dolly_css () {<br/> echo "<br/> <MCE: style type = 'text/CSS '> <! -- <Br/> # Dolly {<br/> position: absolute; <br/> top: 2.3em; <br/> margin: 0; padding: 0; <br/> right: 1em; <br/> font-size: 16px; <br/> color: # f1f1f1; <br/>}</P> <p> --> </MCE: style> <style type = 'text/CSS 'mce_bogus = "1"> # Dolly {<br/> position: absolute; <br/> top: 2.3em; <br/> margin: 0; padding: 0; <br/> right: 1em; <br/> font-size: 16px; <br/> color: # f1f1f1; <br/>}< br/> </style> <br/> "; <br/>}< br/> add_actio N ('admin _ head', 'dolly _ CSS '); <br/>?>

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.