A brief analysis of WordPress plug-in execution flow _php tutorial

Source: Internet
Author: User

1, first, I now Pugins folder to write a plugin of their own
Copy PHP content to clipboard
PHP Code:

/*
Plugin Name:test
Plugin URI: [url=http://wordpress.org/]http://wordpress.org/[/url]#
Description: I used the test.
AUTHOR:LW (Fantasy)
version:0.1
Author URI: [Url=http://www.xxx.com/]http://www.xxx.com/[/url]
*/

$test = "This is my first plugin!";

function output () {
Global $test;
Echo $test;
}

Add_action (Wp_footer,output);
?>


Then enable in the background:

2, WP execution is loaded in "wp-settings.php", and in this file, you can find the following plug-in-related code snippets:
Copy PHP content to clipboard
PHP Code:
if (Get_option (active_plugins)) {
$current _plugins = get_option (active_plugins);
Dump ($current _plugins);
if (Is_array ($current _plugins)) {
foreach ($current _plugins as $plugin) {
if (! = $plugin && 0 = = Validate_file ($plugin) && file_exists (wp_plugin_dir./. $plugin))
Include_once (Wp_plugin_dir./. $plugin);
}
}
}


I dump the $current_plugins and get

Array
(
[0] = fanfou-daily/fanfou-daily.php
[1] = mulberrykit.php
[2] = test.php
)

You can see that the test.php plugin I wrote has been included in the include.

3, in the theme template footer.php inside will execute a function

And this wp_footer is executed inside.

Do_action (Wp_footer);

And this do_action is the implementation of the previous we have registered "add_action (wp_footer,output); The output () function ...

This will output "This is my first plugin!" The

Finally paste the Do_action source code, we appreciate it
Copy PHP content to clipboard
PHP Code:
/**
* Do_action ()-Execute functions hooked on a specific action hook.
*
* This function invokes all functions attached to action hook$tag.
* It is possible to the create new action hooks by simply calling the This function,
* Specifying the name of the new hook using the$tagParameter.
*
* Can pass extra arguments to the hooks and much like you can with Apply_filters ().
*
* @see apply_filters () This function works similar with the exception it is
* Returned and only the functions or methods is called.
*
* @package WordPress
* @subpackage Plugin
* @since 1.2
* @global Array $wp _filter Stores all of the filters
* @global Array $wp _actions increments the amount of times action was triggered.
*
* @param string $tag The name of the action to be executed.
* @param mixed $arg,... Optional additional arguments which is passed on to the functions hooked to the action.
* @return NULL would return NULL if $tag does not exist in $WP _filter array
*/
function Do_action ($tag, $arg =) {
Global $WP _filter, $wp _actions, $merged _filters, $WP _current_filter;
if (Is_array ($WP _actions))
$WP _actions[] = $tag;
Else
$WP _actions = Array ($tag);
$WP _current_filter[] = $tag;
Do all actions first
if (Isset ($WP _filter[all])) {
$all _args = Func_get_args ();
_wp_call_all_hook ($all _args);
}
if (!isset ($wp _filter[$tag])) {
Array_pop ($wp _current_filter);
Return
}
$args = Array ();
if (Is_array ($arg) && 1 = = count ($arg) && is_object ($arg [0])//Array (& $this)
$args [] =& $arg [0];
Else
$args [] = $arg;
for ($a = 2; $a < Func_num_args (); $a + +)
$args [] = Func_get_arg ($a);
Sort
if (!isset ($merged _filters[$tag])) {
Ksort ($WP _filter[$tag]);
$merged _filters[$tag] = true;
}
Reset ($wp _filter[$tag]);
do {
foreach (array) current ($WP _filter[$tag]) as $the _)
if (!is_null ($the _[function))
Call_user_func_array ($the _[function], array_slice ($args, 0, (int) $the _[accepted_args]));
} while (Next ($wp _filter[$tag])!== false);
Array_pop ($wp _current_filter);
}


The key is Call_user_func_array ($the _[function], array_slice ($args, 0, (int) $the _[accepted_args]).

http://www.bkjia.com/PHPjc/508294.html www.bkjia.com true http://www.bkjia.com/PHPjc/508294.html techarticle 1, first, I now Pugins folder to write a copy of their own plug-in PHP content to the Clipboard PHP code:? PHP/* Plugin name:test Plugin URI: [url=http://wordpress.org/]http ://word ...

  • Related Article

    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.