3

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:

$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?

1

4 Answers 4

6

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
  • I do however need to create it not as an admin - i am adding the category as a response to an API response.
    – Neta Meta
    Commented Jan 29, 2014 at 14:39
  • Well i took it out of init and set it in the main scope (out side of my plugin class) and still it did not find it, i ended up using wp_insert_term which seems to work
    – Neta Meta
    Commented Jan 29, 2014 at 14:53
10

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

require_once( ABSPATH . '/wp-admin/includes/taxonomy.php');
1
  • It's also good to check if the function exists first: if( !function_exists('wp_insert_category') ){ require_once( ABSPATH . '/wp-admin/includes/taxonomy.php'); } Commented Nov 5, 2020 at 17:56
5

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
  • A little explanation of why it must be done in admin_init action hook would be great.
    – cybmeta
    Commented Sep 13, 2015 at 9:48
  • Did you try it, without admin_init? Commented Sep 14, 2015 at 15:41
  • 1
    I know why it must be used on admin_init (or any other later action hook) but I'm not asking for the explanation for myself but for any other reader thay may not know why this code work and the other not. That would make your answer a great answer and not only a working answer. Answers consisting only in code are not considered good answers here. Please, read the help center and How do I write a good answer?. That is why, as it is, your answer is badder than the current accepted answer.
    – cybmeta
    Commented Sep 14, 2015 at 19:10
  • Exactly, if you you really want to use wp_insert category and use admin_iti else wp_insert_term is the way. Commented Aug 23, 2016 at 11:42
0

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);
1
  • You might want to at least load wp-load.php from within an absolute path, like you loaded taxonomy.php. And btw, there are other constants for that, that take into account if you moved wp-admin to a different location.
    – kaiser
    Commented Mar 3, 2017 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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