My plugin code is below. In the myPlugin_cleanup() function, I’m creating some categories and assigning term id 1 as the parent.
Due to the known bug with creating categories via script (the category_children array does not get created and results in a fatal error when pages containing category listings are accessed), I’ve placed the clean_term_cache() call directly below the function that creates my categories.
However, its not working. The category_children option is not getting created.
Interestingly, if I create a plugin whose sole function is to execute the clean_term_cache(”,’category’) call, it fixes the problem and creates the category_children option with the correct values.
I’m just not sure why its not working in the same plugin that creates the categories.
What might I be missing?
function myPlugin_activate(){
if ( get_option( 'myPlugin_activated' ) !== "1"){
//comment out any function you don't want to run
myPlugin_copy('themes');
myPlugin_copy('plugins');
myPlugin_images();
myPlugin_options();
myPlugin_settings();
myPlugin_widgets();
myPlugin_links();
myPlugin_posts();
myPlugin_pages();
myPlugin_custom_menus();
myPlugin_cleanup();
clean_term_cache('','category'); //does not work here
update_option('myPlugin_activated', "1");
}
}
function myPlugin_cleanup(){
//include dependencies
require_once(ABSPATH.'/wp-admin/includes/taxonomy.php');
if(!get_cat_ID('nofollow')){wp_create_category('nofollow',1);}
if(!get_cat_ID('noindex')){wp_create_category('noindex',1);}
clean_term_cache('','category'); //Does not appear to work here either.
}
register_activation_hook(__FILE__, 'myPlugin_activate');
?>
After creating about 40 test WP installs today on localhost, here’s what I can report:
It appears that the correct function call to rebuild the category_children options is:
Previously, I was trying to call:
And I was calling it just after my categories were created in my plugin code. However, I could never get this function to do anything when called from the same plugin that I’m using to create the categories. Interestingly, I could place the function in a stand-alone plugin and run it (after my category creation plugin) and it would rebuild the category_parents options array perfectly.
The reason it can’t be called in the same script that creates the categories appears to be related to a peculiar behavior of clean_term_cache() which is best stated by “foofy” in a comment in the bug track ticket:
I hope this helps someone!
I have to do this: