1. Customize
  2. Custom Functionalities
  3. [gamipress_last_achievement_earned] shortcode

[gamipress_last_achievement_earned] shortcode

The following snippet shows how to add a custom shortcode to show the last achievement earned of all or desired achievement types of an user.

Shortcode attributes:

  • achievement_type: Desired achievement types to show the achievement, Accepts: all or any achievement type slug or a comma-separated list of achievement types slugs, Default: all
  • current_user: Set it to yes to show the last achievement earned of current logged in user, Accepts: yes or no, Default: no
  • user_id: Specific user ID to show the last achievement earned (requires current_user=”no”)
  • This shortcode accepts all attributes of [gamipress_achievement] to setup the achievement display.

Examples:
Show the last achievement earned of any achievement type of the current logged in user:
[gamipress_last_achievement_earned current_user="yes"]
Show the last Badge or Quest earned of the current logged in user:
[gamipress_last_achievement_earned achievement_type="badges,quests" current_user="yes"]
Show the last Badge or Quest earned of the user with ID 1:
[gamipress_last_achievement_earned achievement_type="badges,quests" user_id="1"]

Snippet:

function my_prefix_gamipress_last_achievement_earned_shortcode( $atts ) {

    // To render the achievement, this shortcode has support to all attributes of  shortcode
    $atts = shortcode_atts( array_merge( array(
        'achievement_type' => 'all',
        'user_id' => '0',
        'current_user' => 'no'
    ), gamipress_achievement_shortcode_defaults() ), $atts, 'gamipress_last_achievement_earned' );

    // Check desired points types
    if( $atts['achievement_type'] === 'all') {
        $achievement_types = gamipress_get_achievement_types_slugs();
    } else {
        $achievement_types = explode( ',', $atts['achievement_type'] );
    }

    // Force to set current user as user ID
    if( $atts['current_user'] === 'yes' ) {
        $atts['user_id'] = get_current_user_id();
    } else if( absint( $atts['user_id'] ) === 0 ) {
        $atts['user_id'] = get_current_user_id();
    }

    // Get last achievement earned by this user
    $user_achievements = gamipress_get_user_achievements( array(
        'user_id'          => $atts['user_id'],
        'achievement_type' => $achievement_types,
        'limit'            => 1,
    ) );

    if( isset( $user_achievements[0] ) ) {

        // Setup the achievement display options
        $template_args = array(
            'user_id' 	=> $atts['user_id'], // User ID on achievement is used to meet to which user apply earned checks
        );

        // Get the achievement shortcode fields
        $achievement_fields = GamiPress()->shortcodes['gamipress_achievement']->fields;
        unset( $achievement_fields['id'] );

        // Loop achievement shortcode fields to pass to the achievement template
        foreach( $achievement_fields as $field_id => $field_args ) {
            if( isset( $atts[$field_id] ) )
                $template_args[$field_id] = $atts[$field_id];
        }

        // Render the achievement
        return gamipress_render_achievement( $user_achievements[0]->ID, $template_args );
    }

}
add_shortcode( 'gamipress_last_achievement_earned', 'my_prefix_gamipress_last_achievement_earned_shortcode' );
Was this article helpful to you? Yes No

How can we help?