問題描述

客户詢問他使用的特定轉盤插件是否可以插件。這意味着我應該在調用插件功能的 functions.php 中創建一個小工具。這意味着插件的代碼必須首先被加載,以便函數在函數 functions.php 文件加載時可用於 wordpress,對吧?會工作嗎

最佳解決方案

插件在主題之前加載 (是的,我一直在尋找使用這個):

但是考慮到代碼執行點是錯誤的。大多數情況下,一切都應該早於 init 鈎掛鈎並執行。根據 Codex 小工具註冊與 register_widget()應該掛鈎到 widget_init

由於這種負載順序對於這種情況並不重要,因此在任何情況下,您將擁有時間窗口小工具需要的所有東西。

次佳解決方案

一個有趣的方法是以執行順序列出所有鈎子到文件。

add_action( 'all', '_20161224_printer' );
function _20161224_printer( $r ){

    $line =  microtime(true)*10000 . ' ' . $r .  "n";
    $fp = fopen( ABSPATH . 'hooks.txt', 'a+');
    fwrite($fp, $line);
    fclose($fp);

}

你會得到這樣的輸出:

14825992300742 pre_option_blog_charset
14825992300743 option_blog_charset
14825992300743 plugins_loaded
14825992300744 load_default_widgets
14825992300745 load_default_embeds
14825992300745 wp_audio_extensions
14825992300745 wp_audio_embed_handler
14825992300746 wp_video_extensions
14825992300746 wp_video_embed_handler
14825992300746 sanitize_comment_cookies
14825992300747 pre_option_permalink_structure
14825992300747 option_permalink_structure
14825992300748 pre_option_wp_user_roles
14825992300748 option_wp_user_roles
14825992300749 wp_roles_init
14825992300749 setup_theme
14825992300749 pre_option_template
14825992300750 option_template
14825992300750 template
14825992300750 theme_root
14825992300751 template_directory
14825992300751 pre_option_stylesheet
14825992300751 option_stylesheet
14825992300751 stylesheet
14825992300752 theme_root
14825992300752 stylesheet_directory
14825992300752 pre_option_WPLANG
14825992300753 query
14825992300754 default_option_WPLANG
14825992300755 locale
14825992300755 override_unload_textdomain
14825992300755 unload_textdomain
14825992300755 override_load_textdomain
14825992300756 load_textdomain
14825992300756 load_textdomain_mofile
14825992300756 locale
...
many many more action hooks
...
14825992302886 wp_parse_str
14825992302886 nonce_life
14825992302886 salt
14825992302886 wp_parse_str
14825992302887 esc_html
14825992302887 logout_url
14825992302887 clean_url
14825992302887 gettext
14825992302887 wp_after_admin_bar_render
14825992302888 pre_option_template
14825992302888 option_template
14825992302888 template
14825992302888 theme_root
14825992302888 template_directory
14825992302889 parent_theme_file_path
14825992302889 shutdown

Note the full list simply could not fit the 30.000 characters limitation per WPSO post, so I removed many action hooks.

Put the above code inside a plugin. If you do that from the themes functions.php you will not catch plugins_loaded. One another proof the plugins are loaded before the theme.

這個檢查的可能的好處是很多,但是請注意,您將要調用的不同頁面模板的輸出將不同,或者您在儀錶板中。

我簡單地從/?p=1 或 Hello World 頁面調用。

如果您沒有激活一個插件,可以將此代碼放入 mu-plugins 文件夾中。

使用 WP FS API 可能會更好,但是這樣做很簡潔。

第三種解決方案

你可以在插件的.php 文件中有一個小工具 (並且有一個共享的全局變量,它們都可以使用),如果這是你所要求的。這是一個 tutorial with sample code 我偶然發現。

另外,這是一個執行順序的 list of actions run during a typical request 。 #2 和#10 表示插件首先加載; 不知道 functions.php 。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。