Last active
September 10, 2020 03:09
-
-
Save gabrielmerovingi/af9062009965bb6a5dfb to your computer and use it in GitHub Desktop.
This custom shortcode can be used to display myCRED Links which grants points to a user only if the current user has a particular badge. Requires the badge_id="" attribute to be used and contain the Badge ID to check for.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_shortcode( 'mycred_conditional_badge_link', 'mycred_render_conditional_badge_link' ); | |
function mycred_render_conditional_badge_link( $atts, $content ) { | |
// Make sure myCRED is installed | |
if ( ! function_exists( 'mycred' ) ) return 'myCRED is not installed'; | |
// Not applicable for visitors | |
if ( ! is_user_logged_in() ) return; | |
$atts = shortcode_atts( array( | |
'id' => '', | |
'rel' => '', | |
'class' => '', | |
'href' => '', | |
'title' => '', | |
'target' => '', | |
'style' => '', | |
'badge_id' => 0, | |
'ctype' => 'mycred_default', | |
'min_balance' => 0, | |
'hreflang' => '', // for advanced users | |
'media' => '', // for advanced users | |
'type' => '' // for advanced users | |
), $atts ); | |
// Check that user has badge | |
if ( $atts['badge_id'] > 0 && function_exists( 'mycred_get_users_badges' ) ) { | |
$mycred = mycred( $atts['ctype'] ); | |
$cui = get_current_user_id(); | |
if ( $mycred->exclude_user( $cui ) ) return ''; | |
$users_badges = mycred_get_users_badges( $cui ); | |
// If user does not have this badge | |
if ( ! in_array( absint( $atts['badge_id'] ), $users_badges ) ) | |
return ''; | |
} | |
// Let myCRED run the mycred_link shortcode. | |
return mycred_render_shortcode_link( $atts, $content ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment