Adding Categories Through Function

I am creating a template theme for WP and am wondering if there is a way of the theme adding new categories to the db, through the functions.php upon activation of the theme.

It seems like a fairly straight forward thing to do, and can quite easily be done to post formats.

Read More

Of course its simple enough to tell the end users what categories they’ll need to add once the have activated the theme, but I was wondering if that could be a step we could just miss out.

Anyways, thanks for your help, and give me a slap if there is an obvious answer 🙂

Matt

Related posts

1 comment

  1. Themes do not have an equivalent of register_activation_hook() like plugins.

    You may also run into issues during a review if you’re hoping to be hosted on the repo. I’m not sure how creating categories classifies exactly, but doing things like creating post types is explicitly disallowed.

    That said, you can use wp_create_category() in your functions.php file with a check to see if the category exists with term_exists(). Code below is untested.

    if(!term_exists('my_category_name','category')) {
        wp_create_category('my_category_name');
    }
    

Comments are closed.