0

I create custom plugin for import categories. now i have categories array from api.but when import categories into my wordpress woo commerce means not working.

My code is given below.

public function sample_insert_category() {
    if(!term_exists('sample-category')) {
        wp_insert_term(
            'Sample Category',
            'category',
            array(
              'description' => 'This is an sample category.',
              'slug'        => 'sample-category'
            )
        );
    }
}
add_action( 'init', 'sample_insert_category' );

already i have used this code in function.php file. now i create a plugin. so i need to include this to my plugin. When i add this code into my plugin class means i got the warning.

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'admin_menu_top' not found or invalid function name

i want to add category and sub category using code.. help me anyone.

1
  • 1
    Error seems be in another place and not in the code you have provided. Apr 1 '15 at 7:47
1

The syntax for adding an action in WordPress WITHIN a class is somewhat different. Your code will have to look somewhat more like the following...

class MyPluginClass {

    public function __construct() {

         add_action( 'init', array( $this, 'sample_insert_category') );
    }

    public function sample_insert_category() {

        if(!term_exists('sample-category')) {
            wp_insert_term(
                'Sample Category',
                'category',
                array(
                    'description' => 'This is an sample category.',
                    'slug'        => 'sample-category'
                )
            );
        }

    }
}

$mypluginclass = new MyPluginClass(); 
1
  • my condition always false on if(!term_exists('sample-category')) in this line. so i have removed this condition. but its not working for me.. Apr 1 '15 at 8:15
0

Finally i used the following code and i add category from my plug in.

class MyPluginClass {
    public function __construct() {
         add_action( 'init', array( $this, 'sample_insert_category') );
    }


    public function sample_insert_category() {


        if(!term_exists('Test','product_cat')) {

            wp_insert_term(
                'Test',
                'product_cat',
                array(
                    'description' => 'This is an sample category.',
                    'slug'        => 'Test'
                )
            );
        }

    }


}

$mypluginclass = new MyPluginClass(); 

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.