• I am creating a plugin that allows category creation/modification/deletion.

    I was previously using $wpdb to query the database as needed.

    I stumbled across the wp_insert_category(); function… Thought I’d give it a try. It does not seem to be working like it should.

    Here is the code using wp_insert_category();

    //Define the category
    $category_fields = array('cat_name' => $_POST['tag-name'], 'category_description' => $_POST['description'], 'category_nicename' => $_POST['slug'], 'category_parent' => $_POST['category_parent']);
    
    // Create the category
    $category_fields_id = wp_insert_category($category_fields);

    When executing the script, the following error occurs:

    Fatal error: Call to undefined function wp_insert_category()

    When I include the taxonomy.php file:

    require_once(ABSPATH . "wp-admin/includes/taxonomy.php");

    The execution successfully creates the category in full detail.

    Why do I need to include this file in my plugin? Shouldn’t it just work without having to including it?

    -w3dgie

Viewing 1 replies (of 1 total)
  • Use this
    wp_insert_term($category_name, 'category', array(
    'description'=>$description,
    'slug'=>sanitize_title($slug_category_name),
    'parent'=>$parent
    ));

Viewing 1 replies (of 1 total)
  • The topic ‘wp_insert_category() – Working, but not working?’ is closed to new replies.