Lamp development essentials (2): Analysis of WordPress 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 written casually, but in a fixed format. They can be viewed as metadata of the plug-in ). 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 whose header is annotated as follows:

/*
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 system function, it executes the hello_dolly function customized by the plug-in. 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 to display information and define CSS styles.

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.

Usage: Save it as a PHP file, copy it to the plug-in directory, or see a folder and copy it to the folder. In this way, you can see the list of plug-ins in the plug-in management interface, click "enable" to make the plug-in take effect. Then, add <! -- Mycopyright --> comment. After saving the blog, you can view the blog. The comment is replaced with a string in the red font. "This article is original from the mesh. Reprinted by the original author: mesh (Zhang Qing) ", and contains a link.

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

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.