WordPress.org

Ready to get started?Download WordPress

Forums

wp_create_category() from functions.php returns Undefined (4 posts)

  1. vestaviascott
    Member
    Posted 4 years ago #

    I'm seeking to test for the existence of a category (by name) and if the category does not exist, I'd like to create it.

    I'm using the following code in my theme's functions.php file...

    if(get_cat_ID('hidden')!==1){wp_create_category('hidden');}

    However, its returning an undefined function error. I suppose I need to include taxonomy.php in my functions.php?

  2. Bootje
    Member
    Posted 4 years ago #

    I thought the wp_create_category is already checking if the category exists.

  3. WebTechGlobal
    Member
    Posted 3 years ago #

    yes wp_create_category will return the existing category id or false.

    Remember the passed category name must be an exact match.

    Download and Downloads would result in 2 categories.

    Contact WebTechGlobal for more support

  4. Digital Raindrops
    Member
    Posted 3 years ago #

    No need to add the compare, as get_cat_ID returns '0' or the catagory id if found, and as 0 will be seen by the code as false you could use if not ! .

    if(!get_cat_ID('hidden')){wp_create_category('hidden');}

    If that fails let me know as I have a function that will work for this situation, I use in my themes to add default categories, as I wanted to be able to add the parent category by name as well so I created my own function as wp_create_category needs the parent ID and will not accept a name.

    David

Topic Closed

This topic has been closed to new replies.

About this Topic