Skip to main content
We are Brand SEO Beijing serving international business, your marketing partner, Contact us by mi@mgsh.com.cn

Analysis of website CMS plug-in mechanism

I have always been very interested in software components and plug-in architecture. I think that any application developed now must have a well-structured plug-in mechanism, which can attract other people to develop plug-ins and greatly expand the functions of the system. Eclipse is a great example, and you can find corresponding third-party plug-ins from the community for almost any function you want. The same is true of Firefox, its plug-in mechanism is very flexible (especially compared to IE), so a variety of plug-ins can be found in the Firefox community.

1) WordPress site CMS reads all available plugins

In the file "/wp-admin/includes/plugin.php", the function get_plugins() is used to get all the plugins from the file system.The principle is very simple, that is to read all the PHP files in the "wp-content/plugins" directory.This function allows first-level subfolders, that is PHP files under 'wp-content/plugins', and therefore PHP files inside first-level subfolders under this directory to be listed as candidates for plugins, using The following function goes to further extract plugin information.The advantage of this is that it is convenient for users to use folders to manage and organize plug-ins.
The function get_plugin_data() is used to get the description of the plug-in (Plugin Descriptor), which mainly includes the version, name, author, and other information of the plug-in, which actually exist in the form of comments.Take the Hello plugin that comes with WordPress as an example:

Hello, Dolly in the upper right of your admin screen on every page. Author: Matt Mullenweg Version: 1.5 Author URI: http://ma.tt/ */ ?>

In this way, in the get_plugin_data function, you can get the detailed information of the plugin.

'.$plugin.' '; } if ('' == $author_uri[1] ) { $author = trim( $author_name[1] ); } else { $author = ' ' . trim( $author_name[1] ) . ' '; } return array('Name' => $name, 'Title' => $plugin, 'Description' => $description, 'Author' => $author, 'Version' => $version); } ?>

2) Enable & Disable Plugins

The operations for enabling (disabling) plugins are all in Plugins.php. For example, if I want to Deactive "Hello" this plugin, the final URL is actually like this:

http://localhost/blog/wp-admin/plugins.php?action=deactivate&plugin=hello.php

Among them, "Action" represents the action, the value is "active" or "deactivate", and "Plugin" represents the object plug-in of the action, here is "hello.php".After the action instruction is obtained, the currently activated plug-in is firstly retrieved from the database.


Then according to the action, regenerate the activated plug-in array, store it in the database, and reload the page.When loading, you need to consider how these activated plugins work.

BTW: Attach the values ​​for 0 plugins and only 1 plugin in the Options table of the database:
Without plugins:

a:1:{i:0;s:0:"";}

Only the Hello plugin:

a:2:{i:0;s:0:"";i:1;s:9:"hello.php";}

3) How to load enabled plugins into the system

Every page in WordPress includes the "wp-config.php" file, and the "wp-settings.php" file is also automatically loaded in "wp-config.php".In the "wp-settings.php" file, the following plugin-related code snippets can be found:


It can be seen that this code will take out all enabled plugins in the system and include them.So when each page loads, these plugin codes will be included first.So, what do these plugins do when they load themselves?

4) Loading of plugins

In fact, the most important part of plug-in loading is the event registration mechanism of the plug-in. The event registration in the WordPress plug-in is actually very similar to the Extension-Point mechanism in Eclipse. The method of software plugging and unplugging has also become the most widely used practice in software component architecture recently.
Several important functions in the event registration process are: do_action, add_action, and add_filter. Many extension points (also called "hooks") are defined by default in WordPress, or a lot of system events are registered (the formal name in WP should be "Action Tag"), for example "admin_head" means the Head output event of the Admin page , "publish_post" represents the event that publishes a post, etc.What the plug-in needs to do is to extend these extension points, or hook these hooks, so as to realize the extension function of the system. add_action is the function that is usually used by the plugin to extend an extension point, and do_action is the function that the extension point itself starts to execute.
I just said that each page in WordPress will Include all Active plugin code before execution, and these codes usually use "add_action" to register their own functions in the extension point of the system.In this way, when the extension point is executed, all the functions of the plug-ins that have been hooked to the extension point in the system will be found and executed, thereby expanding the functions of the system.
Many functions in WordPress are also implemented through this plugin structure, and many system events are registered by default in 'default-filter.php'.for example:


This is used to send an XML-RPC Ping when publishing each post.Another example:


Used to convert the smiley in the content to an image.

Or take the "Hello" plugin as an example. The Hello plugin will randomly display a paragraph in the upper right corner of the Admin Page. It works like this:
in front of every Admin page


And in "admin-head.php" all extensions of extension point "admin-head" will be executed:


In this way, all functions hooked to admin_head will be executed, and the Footer section of the Admin Page is similar.

5) If UI is involved in the plugin

Actually it's the same.Take adding a menu in the WordPress FeedBurner Plugin as an example.
If you want to add a menu, you need to register the "admin_menu" Action Tag (system event):


This function in the plugin is:


The "add_options_page" function will add a submenu such as "FeedBurner" to the "Options" menu of the system.

6) Other

There are also some simple plugins that only provide some API functions.For example, Most_Commented Plugin, it provides an API "mdv_most_commented": get the most commented articles through database query, and display them.Because this plugin has been Included, it can be displayed using this API.
Source of this article: www.cnblogs.com/vicenteforever/articles/1867214.html

Back to Top