Created
August 19, 2014 09:12
-
-
Save gabrielmerovingi/73b88b6339eba530a93f to your computer and use it in GitHub Desktop.
Custom WordPress shortcode that allows you to enforce a maximum number of clicks a certain URL should award points. Once this max has been reached, the link is no longer shown.
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_max_link', 'mycred_render_max_link' ); | |
function mycred_render_max_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' => '', | |
'amount' => 0, | |
'ctype' => 'mycred_default', | |
'max_clicks' => 0, | |
'hreflang' => '', // for advanced users | |
'media' => '', // for advanced users | |
'type' => '' // for advanced users | |
), $atts ); | |
// Check max click requirement | |
if ( $atts['max_clicks'] > 0 ) { | |
$mycred = mycred( $atts['ctype'] ); | |
global $wpdb; | |
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$mycred->log_table} WHERE ref = 'link_click' AND data LIKE %s;", '%' . $atts['href'] . '%' ) ); | |
// If count has reached max we return nothing. | |
if ( $count >= (int) $atts['max_clicks'] ) | |
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
Does this code still work? how do I use it other than just adding it to functions.php?