Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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');
?>
share|improve this question
    
Update: It appears that the fix for this is to call _get_term_hierarchy('category') NOT clean_term_cache('','category'); –  Scott B Apr 19 '11 at 21:34

2 Answers 2

up vote 3 down vote accepted

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:

_get_term_hierarchy('category')

Previously, I was trying to call:

clean_term_cache('','category')

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:

This is only a problem if you make multiple term changes in the same PHP request/script lifetime. clean_term_cache() tracks each taxonomy it cleans in a static variable and won't clean it a second time. If several term changes are made in the same request, the {$taxonomy}_children option is left with the hierarchy as it was after the first change.

I hope this helps someone!

share|improve this answer

I have to do this:

$taxnomy_name = ...;
clean_term_cache ( _get_term_hierarchy($taxnomy_name), $taxnomy_name);
share|improve this answer
3  
Can you explain that? –  toscho Oct 25 '12 at 13:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.