Advertisement
designbymerovingi

Conditional myCRED Shortcode based on post authors balance.

Aug 21st, 2014
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.18 KB | None | 0 0
  1. add_shortcode( 'mycred_conditional_link', 'mycred_render_conditional_link' );
  2. function mycred_render_conditional_link( $atts, $content ) {
  3.  
  4.     // Make sure myCRED is installed
  5.     if ( ! function_exists( 'mycred' ) ) return 'myCRED is not installed';
  6.  
  7.     $atts = shortcode_atts( array(
  8.         'id'          => '',
  9.         'rel'         => '',
  10.         'class'       => '',
  11.         'href'        => '',
  12.         'title'       => '',
  13.         'target'      => '',
  14.         'style'       => '',
  15.         'amount'      => 0,
  16.         'ctype'       => 'mycred_default',
  17.         'min_balance' => 0,
  18.         'hreflang'    => '',   // for advanced users
  19.         'media'       => '',   // for advanced users
  20.         'type'        => ''    // for advanced users
  21.     ), $atts );
  22.  
  23.     // Check minimum balance requirement
  24.     if ( $atts['min_balance'] > 0 && is_single() ) {
  25.  
  26.         global $post;
  27.         $mycred = mycred( $atts['ctype'] );
  28.         $author_id = $post->post_author;
  29.         $balance = $mycred->get_users_balance( $author_id, $atts['ctype'] );
  30.  
  31.         // If balance is lower than the limit we return nothing.
  32.         if ( $balance < $mycred->number( $atts['min_balance'] ) )
  33.             return '';
  34.  
  35.     }
  36.  
  37.     // Let myCRED run the mycred_link shortcode.
  38.     return mycred_render_shortcode_link( $atts, $content );
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement