• Resolved Kendall Weaver

    (@kendalltristan)


    Hello,

    I’m currently working on a site which has several custom post types, a few of which need to be tracked with BadgeOS for the purposes of awarding badges/points/etc. Let’s say that our custom content types are “wins”, “losses”, and “ties”.

    I’m presently wondering how to set up triggers so that I could create badges for when a user has, for instance, 10 wins or 20 losses or whatever other arbitrary amount I would want to assign. Also I’m wondering how I can set them up as steps so that in order to get a particular badge, a user might need to have 15 wins and 5 ties or some other combination.

    I took a look at this post here: https://wordpress.org/support/topic/badge-for-publishing-a-new-post-of-custom-post-type however a solution is not presented within the post.

    If I can go farther and suggest a means of implementing this so that others can benefit. Instead of having “Publish a new post” and “Publish a new page” as possible steps, why not have “Publish content of type:” and then a drop-down of available content types within that WordPress instance? Just a thought.

    Anyway, thanks in advance for your help, and thanks for a great plugin.

    https://wordpress.org/plugins/badgeos/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Michael Beckwith

    (@tw2113)

    The BenchPresser

    Honestly, I’m not sure exactly how to go about setting up custom triggers myself, it’s something I’d like to learn. Any of the extensions we have that have custom triggers wasn’t done by myself, so that experience is lacking. My best suggestion is to check out one of the other free extensions and see how they do it. I know the Community Addon has a lot of custom triggers available related to BuddyPress and bbPress that you could gather information on. Since you’re looking at CPTs, perhaps check the triggers in the core plugin for the Post post type and see how that’s done.

    Wish I could provide a bit more help than that, but I’m mostly in the same place as you for this specific topic.

    Thread Starter Kendall Weaver

    (@kendalltristan)

    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.

    Michael Beckwith

    (@tw2113)

    The BenchPresser

    Looks good. Some quick notes that may help reduce/improve your code a bit.

    Are you sure you’re wanting to do things on the saving of the post? or just the publishing? There is a dynamic hook of {$action}_{$post_type} or something close to that, that you could use. You could change the hook you’re on to save_custom_type and it’d only run when saving a post in that post type. You would be able to remove the post type checking.

    There’s also publish_custom_type that would be run when publishing a post from custom_type. Essentially any of the post actions, when editing, followed by the post type, would be valid.

    Thread Starter Kendall Weaver

    (@kendalltristan)

    In my case, yes, saving the post matters more than publishing as I have other stuff going on, but I agree that the majority of people would want to consider using publishing rather than saving. Good call.

    Michael Beckwith

    (@tw2113)

    The BenchPresser

    Word.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Creating Steps for custom post types.’ is closed to new replies.