2

I've isolated the source of a fatal error that's been occurring when a brand new site is previewed just after installing and activating the plugin I'm working on.

The problem appears to be that the "category_children" item in the options table is not being created.

Interestingly, it appears that two actions trigger WP to create the category_children option. They are: (1) Activating a theme that contains code to create categories and (2) Simply accessing the category manager.

If I do either of those, after activating the plugin, the category_children option is being created (presumably due to checks in the theme activation or the edit-tags.php load) and the error goes away.

Here's the code I'm using in my plugin to create the categories (executes on plugin activation only):

        if(!get_cat_ID('nofollow')){wp_create_category('nofollow',1);}
        if(!get_cat_ID('noindex')){wp_create_category('noindex',1);}

What am I missing? Do I need to explicitly call a method to have WordPress generate the category_children options array for these items?

UPDATE: Thanks to scribu's help below on the reason for this bug, I've found that the PARTIAL fix for this bug is to place this line just after my wp_create_category() calls are complete:

clean_term_cache('','category'); 

However, this does not work when called in the same plugin that creates the categories. I had to place it in a standalone plugin that's activated separately from the plugin that creates my categories.

2 Answers 2

2

This is a known (and nasty) bug in the taxonomy hierarchy caching code:

http://core.trac.wordpress.org/ticket/14485

Basically, you have to force a refresh by deleting the option.

8
  • Hi Scribu, thanks for posting this. +1 for that! I've been pulling my hair out trying to figure it out. I'll check out the bug report and your suggested workaround.
    – Scott B
    Commented Apr 19, 2011 at 17:42
  • @Scribu: if the option does not exist (verified by viewing options.php) does manually deleting it still work as the workaround?
    – Scott B
    Commented Apr 19, 2011 at 17:45
  • Just to be clear, I just need to call delete_option("category_children") after creating my parented categories? Sorry, but I didn't see a reference to the workaround in the bug track comments.
    – Scott B
    Commented Apr 19, 2011 at 18:06
  • Update: fix is to place this line after the category inserts > clean_term_cache('','category');
    – Scott B
    Commented Apr 19, 2011 at 18:39
  • Update 2: There appears to be a problem when calling the clean_term_cache fix above, from the same plugin, at the same time that the categories are created. I can place the call in a stand alone plugin and run it after the first plugin and it works, just not in the same plugin that creates the cats.
    – Scott B
    Commented Apr 19, 2011 at 19:42
1

Had the same issue. It was solved adding: delete_option("category_children")

After inserting the categories, apparently this flushes the cache for categories. The other workarounds didn't do the trick for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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