Add support for a plugin

It is straightforward to add support for third-party plugins to Achievements. This will let you create achievement actions which are dependant on actions provided by those plugins.

Add a new record to the achievements_actions table. The name column is very important. It must match the first argument of the do_action() call. In this example, we’d be adding support for the follow action taken from the BuddyPress skeleton component:


/* We'll use this do_action call to send the email notification. See bp-example-notifications.php */
do_action( 'bp_example_send_high_five', $to_user_id, $from_user_id );

For full details of the database schema, review the database model, but for your new database record, set:

  • category = “bp_example”
  • is_group_action = 0
  • name = “bp_example_send_high_five”

In your plugin, or your theme’s function.php, add the following code. See where we use bp_example_send_high_five in the function name? Again, this must match your new database record’s name. The beginning of the function’s name also needs to start dpa_handle_action_.

function dpa_handle_action_bp_example_send_high_five() {
	$func_get_args = func_get_args();
	dpa_handle_action( 'bp_example_send_high_five', $func_get_args );
}

function myplugin_set_action_category_name( $category_name, $category ) {
	if ( __( 'Other', 'dpa' ) == $category_name && 'bp_example' == $category )
		return __( "BuddyPress skeleton component", 'bp-example' );
	else
		return $category_name;
}
add_filter( 'dpa_get_addedit_action_descriptions_category_name', 'myplugin_set_action_category_name', 10, 2 );

Custom Post Types

For most plugins, that’s all you have to do. If the action you are adding support for is automatically invoked by WordPress’ custom post types API, there is a little more to add.

Using the draft_to_publish action as an example, we need to swap the logged in user’s ID for the post author’s ID. This is because the content can be published by another user, typically the site admin. If this wasn’t done, the wrong user would unlock the Achievement. We use the dpa_handle_action_user_id action to hook in and change the user ID that Achievements is using:

function dpa_filter_draft_to_publish_action_userid( $user_id, $action_name, $action_func_args ) {
	if ( 'draft_to_publish' != $action_name )
		return $user_id;

	$post = $action_func_args[0];
	return $post->post_author;
}
add_filter( 'dpa_handle_action_user_id', 'dpa_filter_draft_to_publish_action_userid', 10, 3 );

12 Responses to Add support for a plugin

  1. Pingback: Developer documentation added — Achievements for BuddyPress

  2. Pingback: Quick documentation update — Achievements for BuddyPress

  3. stwc says:

    Nice! I just tried this with my heavily modified Buddypress Like, and it worked a treat! Thanks for the doco, Paul. Saved me *hours* of digging.

  4. paul says:

    Hi, How difficult would it be for me to get an event to trigger some shortcode?

    ie, the user logs in once and a tutorial window pops up.
    I have a plugin which pops a window when it encounters shortcode on a page, would it be possible to do something like this?

    I am a very novice coder.

    Many thanks for any advice,

    Paul.

  5. Tyson says:

    What if the plugin doesn’t have a do_action() ? Is it still possible to integrate? Your help is greatly appreciated.

  6. javierarques says:

    Absolutely awesome Paul, maybe the best Buddypress plugin ever …

  7. Ben says:

    Great plugin, I was able to make it work well with my installation. Question is how can I add shortcode or php to call the member’s total score so i can put it next to their name on the members and groups pages?

  8. Shaun Murphy says:

    Hi Paul,

    Achievements is a great plugin! Love the work you’ve done and looking forward to 3.0. Thanks for providing this detailed explanation of how to add support for other plugins/theme actions.

    I must admit though I am a bit of a newbie when it comes to editing plugins in this depth. I have a couple of questions here. In which file will I find the achievements_actions table and where do I add the new record set to it?

    Also, is it just a matter of adding dpa_handle_action_ script below the do_action script of the function you want to add?

    Thanks Paul.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s