0

Was trying to use this today:

WP Create Category https://codex.wordpress.org/Function_Reference/wp_create_category

Trying that (which I also assume it's only going to add terms to the built in "Categories" taxonomy) throws this error:

Fatal error: Call to undefined function wp_create_category() in /yourwebsite/wp-content/themes/fitt/functions.php on line 123

Though I do see it in /wp-admin/includes/taxonomy.php on line 45:

/**
 * Add a new category to the database if it does not already exist.
 *
 * @since 2.0.0
 *
 * @param int|string $cat_name
 * @param int        $parent
 * @return int|WP_Error
 */
function wp_create_category( $cat_name, $parent = 0 ) {
    if ( $id = category_exists($cat_name, $parent) )
        return $id;

    return wp_insert_category( array('cat_name' => $cat_name, 'category_parent' => $parent) );
}

Has anyone ever seen this before? A function coming up as undefined though it appears to be from both the code and Codex documentation?

2

You are most probably trying to use wp_create_category() on the front end, which will lead to the fatal error. wp_create_category() is not available on the front end, only backend.

You should hook your function to at least admin_init where wp_create_category() is available.

Just a note, looking at the source code, wp_create_category() uses category_exists() which is quite expensive to run on every page load, so you would want to run this once like on a plugin or theme activation hook

|improve this answer|||||
  • That was it, I was trying to hook it into load-(page) and still getting the error. Thanks! – Nathan Nov 18 '15 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.