$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Adding Categories Through Function|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Adding Categories Through Function

matteradmin9PV0评论

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

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

Share Improve this question edited Feb 9, 2019 at 0:00 fuxia 107k39 gold badges255 silver badges461 bronze badges asked May 16, 2013 at 17:36 MattEarlyComMattEarlyCom 112 bronze badges 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. – Andrew Bartel Commented May 16, 2013 at 18:02
  • apologies, yeah, it was a typo... what a numpty :P – MattEarlyCom Commented May 16, 2013 at 20:05
Add a comment  | 

1 Answer 1

Reset to default 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');
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far