网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > PHP技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.启用MEMCACHE_COMPRESSED压缩,“.
.基于PHP的AJAX技术实现文件异步上.
.在PHP中使用灵巧的体系结构.
.PHP远程关机操作的代码.
.PHP中动态HTML的输出技术.
.PHP分析.wav文件并绘制png格式的.
.PHP面向对象的标准.
.php中文乱码问题.
.利用递归把多维数组转为一维数组.
.PHP脚本的10个技巧(6).
.PHP的类--功能齐全的发送邮件类.
.配置PHP使之能同时支持GIF和JPEG.
.PHP反射机制实现动态代理的代码.
.用PHP实现登陆验证码(类似条行码.
.使用数据库保存session的方法.
.简单的页面缓冲技术.
.操作Oracle的php类.
.用PHP实现XML备份Mysql数据库.
.使用"函数递归"实现基于php和MyS.
.旧题新貌:PHP截取中文字符串的问.

浅析Wordpress的插件执行流程

发表日期:2008-12-16


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 ( !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']));这句了

上一篇:PHP安全基础原则与方法 人气:45
下一篇:利用ObjMap将多维数组转换成Object 人气:41
浏览全部Wordpress的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐