how is it possible that using wp_insert_category throw a fatal error?

how is it possible that using wp_insert_category throw a fatal error ?

I am using it as explained : http://codex.wordpress.org/Function_Reference/wp_insert_category
with no change except:

Read More
$cat_defaults = array(
    'cat_name' => 'some_name',
    'category_description' => 'as asdfasdf sdf adfa fas f',
    'category_nicename' => '',
    'category_parent' => '',
    'taxonomy' => 'category'
 );
$someSome = wp_insert_category($cat_defaults);

i dont know if its relevant however i execute it on add_action( 'init', array($this, 'registerCustoms') );

And i get the following error :

Fatal error: Call to undefined function wp_insert_category() in /home1/stodeckc/public_html/podio-wp-sync/wp-content/plugins/podio_management/libs/appSync/appSync_custom.php on line 61

Any ideas?

Related posts

4 comments

  1. The init action is the wrong place. This is because init runs on all requests, admin or front-end, but the wp_insert_category function is an admin-side only function. You generally don’t insert categories from the front end.

    Move to a more specific action, one that will be run in the admin side. Probably from your plugin’s admin pages.

  2. If you used wp_insert_category on Front, you must add this:

    require_once( ABSPATH . '/wp-admin/includes/taxonomy.php');
    
  3. you have to use hook admin init

    function _CreateCategory(){
    $my_cat = array('cat_name' => 'Newcategory', 
        'category_description' => 'Descrip',
         'category_nicename' => 'cat-slug',
          'category_parent' => '');
    
    // Create the category
    wp_insert_category($my_cat);
    }
    add_action('admin_init','_CreateCategory');
    

    And that’s it

  4. I think here is not required to admin_init, because this also define on taxonomy.php

    So please use on this way:

    require_once('wp-load.php' );
    require_once(ABSPATH . 'wp-admin/includes/taxonomy.php');
    
    $cat_defaults = array(
        'cat_name' => 'some_name',
        'category_description' => 'as asdfasdf sdf adfa fas f',
        'category_nicename' => '',
        'category_parent' => '',
        'taxonomy' => 'category'
     );
    $someSome = wp_insert_category($cat_defaults);
    

Comments are closed.