个性化阅读
专注于IT技术分析

在WordPress中激活主题激活函数

点击下载

我正在尝试使用after_setup_theme钩子设置主题激活时的图像大小, 但似乎从未真正调用过它。为什么?

if( !function_exists('theme_image_size_setup') )
{
    function theme_image_size_setup()
    {
        //Setting thumbnail size
        update_option('thumbnail_size_w', 200);
        update_option('thumbnail_size_h', 200);
        update_option('thumbnail_crop', 1);
        //Setting medium size
        update_option('medium_size_w', 400);
        update_option('medium_size_h', 9999);
        //Setting large size
        update_option('large_size_w', 800);
        update_option('large_size_h', 9999);
    }
}
add_action( 'after_setup_theme ', 'theme_image_size_setup' );

取而代之的是, 我围绕解决方案进行了工作, 但是如果有一个钩子, 那并不是最佳选择:

if ( is_admin() && isset($_GET['activated'] ) && $pagenow == 'themes.php' ) {
    theme_image_size_setup();
}

这可行…但是为什么after_setup_theme钩子没有响应?


#1


可能问题是你在字符串’after_setup_theme’内有额外的空间。

像这样尝试:

add_action( 'after_setup_theme', 'theme_image_size_setup' );

#2


仅当你的主题已从另一个主题切换到TO时, 此命令才会运行。这是最接近主题激活的方式:

add_action("after_switch_theme", "mytheme_do_something");

或者, 你可以在wp_options表中保存一个选项, 并在每个页面加载中检查该选项, 尽管对我而言似乎效率不高, 但很多人还是建议这样做:

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);
    }
}

#3


after_setup_theme每次页面加载时运行。所以它不是更好的解决方案。


#4


在WPMU环境中使用WP 3.9, 有一个名为switch_theme的操作, 该操作称为切换主题的所有操作。

调用此操作时, 将传递以下$ _GET参数:action = activate, stylesheet =

我在mu-plugins / theme-activation-hook.php中创建了一个新文件

add_action('switch_theme', 'rms_switch_theme');
function rms_switch_theme($new_name, $new_theme='') {
    if (isset($_GET['action']) && $_GET['action'] == 'activate') {
        if (isset($_GET['stylesheet']) && $_GET['stylesheet'] == 'rms') {
            // perform the theme switch processing, // I echo the globals and immediately exit as WP will do redirects 
            // and will not see any debugging output on the browser.
            echo '<pre>'; print_r($GLOBALS); echo '</pre>'; exit;
        }
    } 
}
赞(0)
未经允许不得转载:srcmini » 在WordPress中激活主题激活函数

评论 抢沙发

评论前必须登录!