淺析Wordpress的外掛程式執行流程_PHP教程

來源:互聯網
上載者:User

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 < 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]));這句了

http://www.bkjia.com/PHPjc/508294.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/508294.htmlTechArticle1、首先,我現在pugins檔案夾下寫一個自己的外掛程式 複製PHP內容到剪貼簿 PHP代碼: ?php /* Plugin Name: test Plugin URI: [url=http://wordpress.org/]http://word...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.