删除插件停用时的类别WordPress

时间:2019-12-19 作者:Khurram Ansari

我试图在插件停用时动态删除类别,但它没有删除,但它在激活时正确添加了。唯一的问题是停用时删除类别。

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

1 个回复
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" );

相关推荐

Make taxonomy query dynamic

我对多个页面使用相同的页面模板。-> page-base.php在使用此模板的每个页面上,我想显示使用CPT.我的问题是,如何使数组动态化,以自动更改“分类法”?这是我当前的代码<?php $terms = get_terms( array( \'taxonomy\' => \'catmaison\', \'hide_empty\' => false,