我试图在插件停用时动态删除类别,但它没有删除,但它在激活时正确添加了。唯一的问题是停用时删除类别。
function to add category
function add_category_on_activation(){
global $wpdocs_cat_id;
$wpdocs_cat = array(\'cat_name\' => \'Event Category\', \'category_description\' => \'An Event Category to show all events\', \'category_nicename\' => \'events\', \'category_parent\' => \'\');
$wpdocs_cat_id = wp_insert_category($wpdocs_cat);
}
function to delete category
function delete_category_on_deactivation(){
wp_delete_category($wpdocs_cat_id);
}
Hook to add category
register_activation_hook( __FILE__, "add_category_on_activation" );
Hook to delete category
register_deactivation_hook( __FILE__, "delete_category_on_deactivation" );
SO网友:Chetan Vaghela
您应该将该类别id保存在选项表中,而不是设置为全局。尝试下面的代码,它可以帮助您删除停用挂钩上的类别。
function add_category_on_activation(){
$wpdocs_cat = array(\'cat_name\' => \'Event Category\', \'category_description\' => \'An Event Category to show all events\', \'category_nicename\' => \'events\', \'category_parent\' => \'\');
$wpdocs_cat_id = wp_insert_category($wpdocs_cat);
if($wpdocs_cat_id)
{
update_option(\'wpdocs_cat_id\',$wpdocs_cat_id);
}
}
function delete_category_on_deactivation(){
$wpdocs_cat_id = get_option(\'wpdocs_cat_id\');
if($wpdocs_cat_id)
{
wp_delete_category($wpdocs_cat_id);
delete_option(\'wpdocs_cat_id\');
}
}
register_activation_hook( __FILE__, "add_category_on_activation" );
register_deactivation_hook( __FILE__, "delete_category_on_deactivation" );