王朝网络
分享
 
 
 

浅析Wordpress的插件执行流程

王朝other·作者佚名  2008-12-18
宽屏版  字体: |||超大  

1、首先,我现在pugins文件夹下写一个自己的插件

复制PHP内容到剪贴板

PHP代码:

<?php

/*

Plugin Name: test

Plugin URI: http://wordpress.org/#

Description: 我测试用的

Author: lw(fantasy)

Version: 0.1

Author URI: http://www.xxx.com/

*/

$test = "<div id='my_test'>这是我的第一个插件!</div>";

function output(){

global $test;

echo $test;

}

add_action('wp_footer','output');

?>

然后在后台启用。。

2、WP执行是加载在”wp-settings.php”,而在此文件中,可以找到以下与插件相关的代码片断:

复制PHP内容到剪贴板

PHP代码:

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);

}

}

}

我dump了一下$current_plugins,得到

Array

(

[0] => Fanfou-Daily/Fanfou-Daily.php

[1] => mulberrykit.php

[2] => test.php

)

可以看到我写的test.php插件已经被include进去了。。

3、在主题模板里的footer.php里面会执行一个函数

<?php wp_footer(); ?>

而这个wp_footer里面又执行

do_action('wp_footer');

而这个do_action就是执行前面我们已经注册了的【add_action('wp_footer','output'); 】output()函数。。。

这样就输出了"<div id='my_test'>这是我的第一个插件!</div>"了

最后贴一下do_action的源码,大家体会一下吧

复制PHP内容到剪贴板

PHP代码:

/**

* do_action() - Execute functions hooked on a specific action hook.

*

* This function invokes all functions attached to action hook <tt>$tag</tt>.

* It is possible to create new action hooks by simply calling this function,

* specifying the name of the new hook using the <tt>$tag</tt> parameter.

*

* You can pass extra arguments to the hooks, much like you can with apply_filters().

*

* @see apply_filters() This function works similar with the exception that nothing is

* returned and only the functions or methods are 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 are passed on to the functions hooked to the action.

* @return null Will 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);

}

其中比较关键的就是call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));这句了

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有