我正在写一个补充插件Wordpress Mobile Pack 因为我想在指定了特定页面模板的页面上使用默认主题。我的插件将只处理该插件的移动切换器部分,因此可以忽略其他组件(我认为)。
查看的源wpmp_switcher.php
, 所有的add_action
和add_filter
相关调用不在函数中,而是在包含文件时执行,如下所示:
add_action(\'init\', \'wpmp_switcher_init\');
add_action(\'admin_menu\', \'wpmp_switcher_admin_menu\');
add_action(\'wp_footer\', \'wpmp_switcher_wp_footer\');
add_filter(\'stylesheet\', \'wpmp_switcher_stylesheet\');
add_filter(\'template\', \'wpmp_switcher_template\');
add_filter(\'option_home\', \'wpmp_switcher_option_home_siteurl\');
add_filter(\'option_siteurl\', \'wpmp_switcher_option_home_siteurl\');
通过使用plugins_loaded
挂钩:function remove_all_wpmp_switchers() {
remove_action(\'init\', \'wpmp_switcher_init\');
remove_action(\'admin_menu\', \'wpmp_switcher_admin_menu\');
remove_action(\'wp_footer\', \'wpmp_switcher_wp_footer\');
remove_filter(\'stylesheet\', \'wpmp_switcher_stylesheet\');
remove_filter(\'template\', \'wpmp_switcher_template\');
remove_filter(\'option_home\', \'wpmp_switcher_option_home_siteurl\');
remove_filter(\'option_siteurl\', \'wpmp_switcher_option_home_siteurl\');
}
add_action(\'plugins_loaded\', \'remove_all_wpmp_switchers\');
到目前为止,一切顺利。现在,我想使用以下代码在不使用我的模板时添加回操作/过滤器:
function wpmp_switcher_exclusions_init(WP_Query $wp_query) {
$template_file_name = get_post_meta($wp_query->get_queried_object_id(),
\'_wp_page_template\', true);
if ($template_file_name != \'my-responsive-template.php\') {
wpmp_switcher_init();
add_action(\'admin_menu\', \'wpmp_switcher_admin_menu\');
add_action(\'wp_footer\', \'wpmp_switcher_wp_footer\');
add_filter(\'stylesheet\', \'wpmp_switcher_stylesheet\');
add_filter(\'template\', \'wpmp_switcher_template\');
add_filter(\'option_home\', \'wpmp_switcher_option_home_siteurl\');
add_filter(\'option_siteurl\', \'wpmp_switcher_option_home_siteurl\');
}
}
add_action(\'parse_query\', \'wpmp_switcher_exclusions_init\');
问题是,即使早在parse_query
hook是,thewpmp_switcher_init
函数调用太晚。这个functions.php
桌面模板和移动模板的文件都试图加载,由于重新定义了函数,导致致命错误。如果可以在此之前检索模板,我不确定如何进行检索。看起来我需要使用setup_theme
钩如果我能想出如何做到这一点,我可能只需要插件中的一个函数,因为setup_theme
之前已调用init
.
非常感谢您的帮助!