首页 php教程 php手册 浅析Wordpress的插件执行流程

浅析Wordpress的插件执行流程

Jun 13, 2016 am 10:34 AM
wordpress 复制 执行 插件 文件夹 流程 现在 自己的 首先

1、首先,我现在pugins文件夹下写一个自己的插件
复制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 = "

这是我的第一个插件!
";

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里面会执行一个函数

而这个wp_footer里面又执行

do_action(wp_footer);

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

这样就输出了"

这是我的第一个插件!
"了

最后贴一下do_action的源码,大家体会一下吧
复制PHP内容到剪贴板
PHP代码:
/**
* do_action() - Execute functions hooked on a specific action hook.
*
* This function invokes all functions attached to action hook $tag.
* It is possible to create new action hooks by simply calling this function,
* specifying the name of the new hook using the $tag 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   $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]));这句了

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

wordpress文章列表怎么调 wordpress文章列表怎么调 Apr 20, 2025 am 10:48 AM

有四种方法可以调整 WordPress 文章列表:使用主题选项、使用插件(如 Post Types Order、WP Post List、Boxy Stuff)、使用代码(在 functions.php 文件中添加设置)或直接修改 WordPress 数据库。

wordpress屏蔽ip的插件有哪些 wordpress屏蔽ip的插件有哪些 Apr 20, 2025 am 08:27 AM

WordPress 屏蔽 IP 的插件选择至关重要。可考虑以下类型:基于 .htaccess:高效,但操作复杂;数据库操作:灵活,但效率较低;基于防火墙:安全性能高,但配置复杂;自行编写:最高控制权,但需要更多技术水平。

wordpress网站账号登录 wordpress网站账号登录 Apr 20, 2025 am 09:06 AM

登录 WordPress 网站账号的步骤:访问登录页面:输入网站网址加上 "/wp-login.php"。输入用户名和密码。点击“登录”。验证两步验证(可选)。成功登录后,您将看到网站仪表盘。

wordpress出现错误怎么办 wordpress出现错误怎么办 Apr 20, 2025 am 11:57 AM

WordPress 错误解决指南:500 内部服务器错误:禁用插件或检查服务器错误日志。404 未找到页面:检查 permalink 并确保页面链接正确。白屏死机:增加服务器 PHP 内存限制。数据库连接错误:检查数据库服务器状态和 WordPress 配置。其他技巧:启用调试模式、检查错误日志和寻求支持。预防错误:定期更新 WordPress、仅安装必要插件、定期备份网站和优化网站性能。

wordpress评论怎么显示 wordpress评论怎么显示 Apr 20, 2025 pm 12:06 PM

WordPress 网站中启用评论功能:1. 登录管理面板,转到 "设置"-"讨论",勾选 "允许评论";2. 选择显示评论的位置;3. 自定义评论表单;4. 管理评论,批准、拒绝或删除;5. 使用 <?php comments_template(); ?> 标签显示评论;6. 启用嵌套评论;7. 调整评论外形;8. 使用插件和验证码防止垃圾评论;9. 鼓励用户使用 Gravatar 头像;10. 创建评论指

wordpress主题头部图片如何更换 wordpress主题头部图片如何更换 Apr 20, 2025 am 10:00 AM

更换 WordPress 主题头部图片的分步指南:登录 WordPress 仪表盘,导航至“外观”>“主题”。选择要编辑的主题,然后单击“自定义”。打开“主题选项”面板并寻找“网站标头”或“头部图片”选项。单击“选择图像”按钮并上传新的头部图片。裁剪图像并单击“保存并裁剪”。单击“保存并发布”按钮以更新更改。

wordpress怎么写页头 wordpress怎么写页头 Apr 20, 2025 pm 12:09 PM

在WordPress中创建自定义页头的步骤如下:编辑主题文件“header.php”。添加您的网站名称和描述。创建导航菜单。添加搜索栏。保存更改并查看您的自定义页头。

wordpress怎么加评论框 wordpress怎么加评论框 Apr 20, 2025 pm 12:15 PM

在 WordPress 网站上启用评论功能,可以为访客提供参与讨论和分享反馈的平台。为此,请按照以下步骤操作:启用评论:在仪表盘中,导航至“设置”>“讨论”,并选中“允许评论”复选框。创建评论表单:在编辑器中,单击“添加块”并搜索“评论”块,将其添加到内容中。自定义评论表单:通过设置标题、标签、占位符和按钮文本来定制评论块。保存更改:单击“更新”以保存评论框并将其添加到页面或文章中。

See all articles