• Resolved brad3260

    (@brad3260)


    Hello everyone, I am developing a plugin and I want to give some defaults categories within my custom taxonomy. I want the user to be able to delete/edit these defaults if they’d like so unless I’m wrong (very possible), I need to add these categories when my plugin gets activated and this is where I am running into problems. Take the following code.

    function bbradley_rfp_activation() {
    	$my_cat = array('cat_name' => 'My Category', 'category_description' => 'A Cool Category', 'category_nicename' => 'category-slug', 'taxonomy' => 'Services');
    
    // Create the category
    $bbt = wp_insert_category($my_cat);
    mail($myemail, 'sub', $bbt);
    
    }
    //add_action("admin_init", 'bbradley_rfp_activation');
    register_activation_hook( __FILE__, 'bbradley_rfp_activation');
    

    When I try to insert the category on the activation of my plugin, it doesn’t work. But if I do it using the admin_init hook, it works but then it can’t be deleted since it would be added every time they visit the admin page. Can someone tell me what I’m doing wrong?

Viewing 12 replies - 1 through 12 (of 12 total)
  • It should work very fine, but make sure that line lies inside the main plugin file
    register_activation_hook( __FILE__, 'bbradley_rfp_activation');

    Thread Starter brad3260

    (@brad3260)

    It is but it’s not working, any ideas why?

    Moderator bcworkz

    (@bcworkz)

    The problem is that when your plugin’s registered hook fires, your plugin has yet to actually be activated, so there is no custom taxonomy yet to add the term to. I suppose you could also register the taxonomy within this callback. You’d still need to do so in regular plugin code as well.

    I think a more elegant solution, though still a little goofy, is to have your plugin’s activation hook callback simply add the “bbradley_rfp_activation” function to the “activated_plugin” action, which of course does fire after your plugin is activated, so your taxonomy will be registered and you should be able to add a term to it.

    Because the hook to “activated_plugin” is only added when your plugin is activated, there would be no issue with other activations firing the same action, which does happen. When that does happen, your “activated_plugin” callback is not there because it is never added when other plugins activate, only yours.

    In a manner of speaking, “activated_plugin” could be thought of as a public action, whereas your plugin’s registered activation hook is “private” to only your plugin. This is only an analogy, the public and private terms are not used in the formal PHP code sense, they are only conceptual terms in this case.

    Thread Starter brad3260

    (@brad3260)

    Hello, thank you for the suggestion. Unfortunately there is very little documentation to this action so I’m hoping you can help me a little more. I replaced the register_activation_hook line with
    do_action('activated_plugin', array(__FILE__, 'bbradley_rfp_activation'));
    but the function doesn’t get triggered?

    Moderator bcworkz

    (@bcworkz)

    Not really what I had in mind ??

    register_activation_hook( __FILE__, 'bbradley_pre_activation');
    function bbradley_pre_activation() {
       add_action('activated_plugin', 'bbradley_rfp_activation');
    }

    The activated_plugin action fires immediately after activation. If this doesn’t work, maybe finding a later hook would do it.

    Thread Starter brad3260

    (@brad3260)

    Ah that makes sense. I have the following code.

    function bbradley_rfp_activation() {
    	$my_cat = array('cat_ID' => 0, 'cat_name' => 'My Category', 'category_description' => 'A Cool Category', 'category_nicename' => 'category-slug', 'taxonomy' => 'Services');
    $bbt = wp_insert_category($my_cat);
    mail($myemail, 'sub', 'post fired');
    }
    
    register_activation_hook( __FILE__, 'bbradley_rfp_activation_pre');
    function bbradley_rfp_activation_pre() {
    	add_action('activated_plugin', 'bbradley_rfp_activation');
    	mail($myemail, 'sub', 'pre fired');
    }

    I get the “pre fired” e-mail but I don’t get the “post fired” e-mail so it seems the “activated_plugin”action is not getting triggered?

    Thread Starter brad3260

    (@brad3260)

    I was mistaken, I am getting both e-mails but the category is not being added.

    Try this; call the function that registers the taxonomy within “bbradley_rfp_activation()”
    Something like this:

    add_action( 'init', 'create_service_tax', 1 );
    
    function create_service_tax() {
    	register_taxonomy(
    		'service',
    		'post',
    		array(
    			'label' => __( 'Service' ),
    			'rewrite' => array( 'slug' => 'services' ),
    			'hierarchical' => true,
    		)
    	);
    }
    
    function bbradley_rfp_activation() {
    	$my_cat = array('cat_name' => 'My Category',
      'category_description' => 'A Cool Category',
      'category_nicename' => 'cool-category', 'taxonomy' => 'services');
    // Rgister Services Taxonomy
    create_service_tax();
    // Create the category
    $bbt = wp_insert_category($my_cat);
    mail($myemail, 'sub', $bbt);
    }
    Thread Starter brad3260

    (@brad3260)

    Problem with that is, the category would be added every time a page is loaded. I was to give the user a couple of default categories that they can keep, edit or delete if they want. I’m starting to think the easiest way to do this is to have a “first time activated” flag that once the plugin is activated, the flag gets set to false. Then in have the plugin check that flag, if true, insert categories, and if false, don’t. I was just trying not to have that if statement run every time, seems like a waste.

    No, it will be added on activation only, the idea here is to register the taxonomy within the activation hook to be able to create terms for this taxonomy.

    Thread Starter brad3260

    (@brad3260)

    You guys rock! That works, thank you so much for the help!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘insert category’ is closed to new replies.