Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Promote Based on Content Purchase
- * Changes a users role based on their myCRED content purchases.
- * @version 1.0
- */
- add_filter( 'mycred_add_finished', 'mycred_me_rolechange_buy_content', 99, 3 );
- function mycred_me_rolechange_buy_content( $reply, $request, $mycred ) {
- // Only applicable if the user gained points for purchasing content
- if ( $reply === false || $request['ref'] != 'buy_content' ) return $reply;
- // Exclude admins
- if ( user_can( $request['user_id'], 'manage_options' ) ) return $reply;
- extract( $request );
- $new_role = 'vip_role';
- $requirement = 9000;
- $user = get_userdata( $user_id );
- global $wpdb;
- // How many points have the user spent on buying points?
- $total = $wpdb->get_var( $wpdb->prepare( "SELECT SUM( creds ) FROM {$mycred->log_table} WHERE ref = 'buy_content' AND user_id = %d AND ctype = %s;", $user_id, $type ) );
- // If no results are found, use the amount paid now
- if ( $total === NULL )
- $total = $amount;
- // Change users role if they have spent the required amount and do not yet have
- // the new role.
- if ( abs( $total ) >= $requirement && ! in_array( $new_role, $user->roles ) )
- wp_update_user( array(
- 'ID' => $user_id,
- 'role' => $new_role
- ) );
- return $reply;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement