categories - delete category on plugin deactivation wordpress

I am trying to delete the category dynamically when the plugin is deactivated but it is not deleting But it is adding on

I am trying to delete the category dynamically when the plugin is deactivated but it is not deleting But it is adding on activation properly the only problem is deletion of category on deactivation.

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

I am trying to delete the category dynamically when the plugin is deactivated but it is not deleting But it is adding on activation properly the only problem is deletion of category on deactivation.

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" );
Share Improve this question asked Dec 19, 2019 at 16:54 Khurram AnsariKhurram Ansari 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

You should save that category id in option table instead of set as global. Try below code which is helps you to delete that category on deactivation hook.

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744888623a4599269.html

相关推荐

  • categories - delete category on plugin deactivation wordpress

    I am trying to delete the category dynamically when the plugin is deactivated but it is not deleting But it is adding on

    1天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信