快速业务通道

浅析Wordpress的插件执行流程

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-05-25

1、首先,我现在pugins文件夹下写一个自己的插件
复制PHP内容到剪贴板
PHP代码:

<?php 
/*
Plugin Name: test
Plugin URI: [url=http://wordpress.org/]http://wordpress.org/[/url]#
Description: 我测试用的
Author: lw(fantasy)
Version: 0.1
Author URI: [url=http://www.xxx.com/]http://www.xxx.com/[/url] 
*/ 
 
$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 (

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号