下面的函数应该在激活它所属的主题时触发。但是,要让它实际创建条件内的类别,我必须(1)激活主题,(2)激活任何其他主题(3)再次激活主题
什么给予,它应该在第一次激活它时处理。
// with activate make sure utility categories are created and parented correctly
if ( is_admin() && isset($_GET['activated'] ) && $pagenow == 'themes.php' ) {
if (file_exists(ABSPATH.'/wp-admin/includes/taxonomy.php'))
{
require_once(ABSPATH.'/wp-admin/includes/taxonomy.php');
wp_create_category('nofollow');
$myCategory1['cat_ID'] = get_cat_id('nofollow');
$myCategory1['category_parent'] = 1;
wp_update_category($myCategory1);
wp_create_category('noindex');
$myCategory2['cat_ID'] = get_cat_id('noindex');
$myCategory2['category_parent'] = 1;
wp_update_category($myCategory2);
}
}
答案 0 :(得分:1)
我在你的另一个问题中回答了这个问题,然后在这里看到了。希望同样的答案适用并适用于您的重新激活难题。尝试使用这个钩子而不是你的$ _GET请求,也许你需要把它作为一个插件,而不是把它放在你的functions.php文件中,以便在主题被激活时运行它。以前的答案如下:
您需要使用动作挂钩。具体来说,'switch_theme'。这是all action hooks的codex页面,我无法专门链接到switch_theme,但向下滚动,你会发现它。关于此钩子没有具体信息,但使用很简单。您可以在functions.php或插件文件中包含您的函数,并在函数定义之后包含this hook:
function add_my_categories($my-theme-name){
//if $my-theme-name == 'my_theme_name
//test if category exists
//if exists, update
//if doesn't exist, create and assign parent
}
add_action('switch_theme','add_my_categories');
'add_action()'调用将在wordpress中遇到命名钩子时运行命名函数。 “switch_theme”挂钩在主题更改后运行。
重要的是要知道这个钩子会为你的函数提供新当前主题的名称,如果你需要它可以接受它作为参数。例如,要确保仅在主题是激活的主题时才运行该功能。我想如果这个函数在你的主题的functions.php文件中,它将永远不会运行,除非你的主题被激活,所以你可以确定是否需要仔细检查主题名称。