如何从子主题自动激活插件

时间:2011-03-03 作者:Ben Ackles

(从重新发布Theme Hybrid Community)

让我们假设一个或多个插件托管在WordPress上。组织。你能自动激活具有这些功能的插件吗。php的子主题?做这样的事有什么问题吗?

向子主题添加插件功能的最佳方式是什么?

2 个回复
最合适的回答,由SO网友:bueltge 整理而成

在主题中包含此函数并使用此挂钩函数

wp_register_theme_activation_hook($code, $function) {
    $optionKey="theme_is_activated_" . $code;
    if(!get_option($optionKey)) {
        call_user_func($function);
        update_option($optionKey , 1);
    }
}
功能和示例:

<?php
/**
 * Provides activation/deactivation hook for wordpress theme.
 *
 * @author Krishna Kant Sharma (http://www.krishnakantsharma.com)
 *
 * Usage:
 * ----------------------------------------------
 * Include this file in your theme code.
 * ----------------------------------------------
 * function my_theme_activate() {
 *    // code to execute on theme activation
 * }
 * wp_register_theme_activation_hook(\'mytheme\', \'my_theme_activate\');
 *
 * function my_theme_deactivate() {
 *    // code to execute on theme deactivation
 * }
 * wp_register_theme_deactivation_hook(\'mytheme\', \'my_theme_deactivate\');
 * ----------------------------------------------
 * 
 * 
 */

/**
 *
 * @desc registers a theme activation hook
 * @param string $code : Code of the theme. This can be the base folder of your theme. Eg if your theme is in folder \'mytheme\' then code will be \'mytheme\'
 * @param callback $function : Function to call when theme gets activated.
 */
function wp_register_theme_activation_hook($code, $function) {
    $optionKey="theme_is_activated_" . $code;
    if(!get_option($optionKey)) {
        call_user_func($function);
        update_option($optionKey , 1);
    }
}

/**
 * @desc registers deactivation hook
 * @param string $code : Code of the theme. This must match the value you provided in wp_register_theme_activation_hook function as $code
 * @param callback $function : Function to call when theme gets deactivated.
 */
function wp_register_theme_deactivation_hook($code, $function) {
    // store function in code specific global
    $GLOBALS["wp_register_theme_deactivation_hook_function" . $code]=$function;

    // create a runtime function which will delete the option set while activation of this theme and will call deactivation function provided in $function
    $fn=create_function(\'$theme\', \' call_user_func($GLOBALS["wp_register_theme_deactivation_hook_function\' . $code . \'"]); delete_option("theme_is_activated_\' . $code. \'");\');

    // add above created function to switch_theme action hook. This hook gets called when admin changes the theme.
    // Due to wordpress core implementation this hook can only be received by currently active theme (which is going to be deactivated as admin has chosen another one.
    // Your theme can perceive this hook as a deactivation hook.
    add_action("switch_theme", $fn);
}
有关更多信息,请参阅this post

SO网友:Rarst

虽然这在技术上是很有可能的,但我认为这是一个糟糕的工作流。这里基本上有两种情况:

没有特定的插件,主题就无法完全发挥作用。我认为总的来说这是不需要的,如果绝对需要的话,将插件的功能移植到主题本身是有意义的。

主题利用插件实现高级功能。不难检测插件是否正在运行(is_plugin_active(), function_exists(), 等),并有条件地运行代码或通知特定功能使用插件,而不是本机可用。

结束

相关推荐

How do you debug plugins?

我对插件创作还很陌生,调试也很困难。我用了很多echo,它又脏又丑。我确信有更好的方法可以做到这一点,也许是一个带有调试器的IDE,我可以在其中运行整个站点,包括插件?