Missing “category_children” option when dynamically creating categories via a plugin

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.

Read More

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.

Related posts

Leave a Reply

2 comments

  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.