1

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.

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

3
  • You are talking about the activation of both a theme and a plugin. What is the relationship?
    – s_ha_dum
    Commented May 16, 2013 at 17:59
  • I assumed plugin was a typo and he meant theme, maybe not though. Commented May 16, 2013 at 18:02
  • apologies, yeah, it was a typo... what a numpty :P Commented May 16, 2013 at 20:05

1 Answer 1

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');
}
1
  • 'Fatal error: Call to undefined function wp_create_category()' I have tried it before, but I can't seem to find the solution, and a google search doesn't really help me Commented May 16, 2013 at 20:12

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.