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" );
Related Posts:
- add_action hook for completely new post?
- How can i list current author’s categories?
- How to Make a Category Always Selected?
- anything like add_meta_box for categories?
- Calling clean_term_cache() fails when called in the same plugin that creates terms, succeeds when called separately?
- Missing “category_children” option when dynamically creating categories via a plugin
- Can we create a category list page in WordPress?
- prevent post to have multiple categories in my plugin
- Show add_meta_box by selecting a specific category
- Which are the hooks run before/after when a category’s deletion?
- Use wp_set_post_terms() instead of wp_insert_post()
- how do you prevent showing a particular category on the admin dashboard for specific user roles?
- Is it possible to change a term slug before being saved to the database?
- wp_insert_category() setting the ‘cat_ID’ gives not array error
- $_FILES empty on created_{taxonomy} and create_{taxonomy} yet is is not on edit_{taxonomy}
- Send push when new post is published in a certain category
- Why is the WordPress taxonomy not registering?
- How do I get the sub categories of the parent when in a sub category?
- How to limit number of number of categories displayed by categories widget
- Translate slug in WPMU with PO files
- Filter Categories widget to allow custom sorting?
- Redirect to another page using contact form 7? [closed]
- Function/Class to list categories with checkboxes
- How can I show posts for a single category?
- Check if post belongs to any category
- How to display the categories on page using shortcode?
- How to display the category id along with category name on categories list?
- add_action pre_term_description vs. pre_category_description
- Non editable custom taxonomy
- How to show parents of current page category, excluding the category?
- How to create taxonomy without using register_taxonomy () function
- Add location tag to wordpress posts
- WP Dropdown Categories, display subcategories but not grandchildren categories?
- Using init hook for register_taxonomy is causing invalid_taxonomy in wp_insert_term()
- Load stylesheet in edit category page?
- How to relate the Category to user?
- How to exclude categories from get_categories() select list inside a widget admin panel
- How to Show Category List With Corresponding Links?
- Exclude Woocommerce Product Category From Sitemap
- How to show only the last two categories in a menu?
- Visual composer custom element error
- Determine if term is Category or Tag
- How to check category name available or not
- get the term_id of current taxonomy page
- How to limit number of category term display in home page grids?
- Post is uncatagorized in publish_post action
- Hide post by ‘post_category’
- Woocommerce product not appearing in category list page when created programatically [closed]
- Yoast and another plugin
- set a custom post type to a taxonomy term programmatically in metabox
- Making Woocommerce optimized for more than 500k products
- Add a new tag based on the category name in the publish event
- add_action priority and taxonomy used in plugin
- Creating an archive page listing external data
- Access WordPress Tag Function From Modal
- What method should I use to store my plugin data (multi level menus with options on each item)
- How to display custom taxonomy and terms using/creating plugin template
- How can i call from custom fields to the category editor?
- What method would I use to show an image in between X amount of posts on category archive? [duplicate]
- filter on get_posts efficiently
- How to get recent one post from each category on home page?
- How do I programatically insert a new menu item?
- jQuery in header or footer
- Notice: _usort_terms_by_ID is deprecated since version 4.7.0! Use wp_list_sort instead
- How to add a new plugin page under desired Options page?
- Plugin screenshots not showing in the repository
- How to make WordPress use non-greedy shortcode parsing?
- Multiple Parameters for a Shortcode
- Multiple entries in get_option results? or why is _multiwidget set to 1?
- Check if I am in the Admin Panel (wp-admin)?
- Fully automated theme install and activation via a plugin
- Generating User(s) with Settings API
- New categories are now going to 404s
- “Reversable” and “Re-useable” Subcategories (or other taxonomic structure)
- Should action callbacks start with a verb?
- How insert negative values in Gutenberg box-control
- What filter should I use to insert a button inside on Media>Add New
- How to use a hook to override an update_post_meta call in a plugin?
- External Authentication, session_tokens not destroyed on logout
- In woocommerce cart item we can add custom text but how can i add custom link? [closed]
- Secure REST with nonce works only when not logged
- template_include filter does not render the custom template
- Why don’t posts show up in my categories on the frontend?
- WordPress plugin enqueue scripts working for js, but not css files
- my basic php code producing unexpected results
- get_plugins() is not give plugin list after performed delete_plugins()
- Best practice for Designing a Plugin with this scenario
- Referencing files in JavaScript in WordPress Plugin
- Borrowing of Previously Translated Strings by Child Plugin
- Add a new category for posts
- Creating a WP plugin and having issues linking CSS stylesheet within functions
- How to enhance a self developed plugin by its own plugin architecture
- What happens/fires when you select a block in the editor?
- How to trigger WooCommerce user profile edit change?
- Stop unauthorised file access
- How to pass a more variable with page/subpage in Wordprees URL?
- Dynamic sidebar areas not working on the Theme Customizer
- Merge Codes using redux framework
- Getting Plugin to read all script files
- wp_insert_post_data filter to set category