For the sake of reference for anyone who might be looking for this information in the future, here is how you add triggers/steps for custom post types:
1. The first step is to register a trigger. Registering a trigger is actually quite simple. Essentially you just extend an array of existing triggers that can be accessed via the badgeos_activity_triggers
filter:
function custom_trigger($triggers) {
$triggers['new_trigger'] = 'New Trigger';
return $triggers;
}
add_filter('badgeos_activity_triggers', 'custom_trigger');
You can add any arbitrary triggers you want to using the above. They don’t actually do anything yet, but they’ll show up in the list when you’re adding a new achievement/badge.
2. Now we need to create a function that tells that trigger when to increment. The simplest means of incrementing the trigger requires simply performing an action of the same name. Thus the trigger could technically be made functional by using the following:
function increment_custom_trigger() {
do_action('new_trigger');
}
And then calling the function either explicitly from somewhere else in your code, or with any appropriate action/filter. Because we’re working with a custom post type, presumably a quantity of them, we can use the save_post
action along with a quick test for the post type to decide whether to increment the trigger:
function increment_custom_trigger($post_id) {
// Get the post object and use the object to determine the type.
$post = get_post($post_id);
$post_type = $post->post_type;
// Test for the type using whatever slug you've assigned it.
if ($post_type == 'custom_type') {
do_action('new_trigger');
}
}
add_action('save_post', 'increment_custom_trigger');
This works for any content posted by the user who is currently logged in as it’s that user whose ID will be passed to BadgeOS. If your users are creating their own content, then this or some slight variation might be all that you need. If, however, you’re awarding achievements/badges based on automatically generated content or content added by other users, something a little more verbose is required.
3. Updating the trigger count for content not explicitly published by the currently logged in user requires incrementing the trigger count, logging that you’ve done so, and then testing against available achievements to see if the user deserves to be awarded anything. A good example of this can be found in the badgeos_trigger_event()
function in the includes/triggers.php
file in the plugin. Using this, we can then modify our above function to act as we need it to:
function increment_custom_trigger($post_id) {
// Set the database as a global variable.
global $wpdb;
// Get the post object and use the object to determine the type and author.
$post = get_post($post_id);
$post_type = $post->post_type;
$post_author = $post->post_author;
// Get the author as an object so we can retrieve a username.
$user = get_userdata( $post_author );
$username = $user->user_login;
// Test for our custom post type slug
if ($post_type == 'custom_type') {
// Iterate the trigger count and save the new count as a variable.
$new_count = badgeos_update_user_trigger_count($post_author, 'new_trigger');
// Log the new trigger count.
badgeos_post_log_entry( null, $author, null, sprintf('%1$s triggered %2$s (%3$dx)', $username, 'new_trigger', $new_count));
// Here's where things get fun. We're going to search the
// database for achievements that use the trigger that we've
// just incremented and save them in an array.
$triggered_achievements = $wpdb->get_results( $wpdb->prepare(
"
SELECT post_ID
FROM $wpdb->postmeta
WHERE meta_key = '_badgeos_trigger_type'
AND meta_value = %s
",
'new_trigger'
) );
// Now we iterate through that array and use an existing
// function to determine if we should award any of those
// achievements to the author of the post.
foreach ($triggered_achievements as $achievement) {
badgeos_maybe_award_achievement_to_user( $achievement->post_ID, $post_author, 'new_trigger', get_current_blog_id(), '' );
}
}
}
add_action('save_post', 'increment_custom_trigger');
And if we haven’t screwed anything up, then it should work as advertised. You should now be able to add a step called “New Trigger”, specify a count and have it accurately award achievements based on that count. You can easily modify the above with a switch
statement or some elseif
statements to test for multiple custom post types.
Cheers.