It should work very fine, but make sure that line lies inside the main plugin file
register_activation_hook( __FILE__, 'bbradley_rfp_activation');
It is but it’s not working, any ideas why?
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.
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?
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.
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?
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);
}
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.
You guys rock! That works, thank you so much for the help!