Skip to content

Instantly share code, notes, and snippets.

@gabrielmerovingi
gabrielmerovingi / Unpublish posts for negative balances
Created March 13, 2014 14:45
This is a simple example where we unpublish a users posts if their account goes minus 20 points.
/**
* Unpublish users Posts
* If a user reaches or exceeds the minimum balance
* we unpublish all their posts.
* @version 1.0
*/
add_filter( 'mycred_add', 'mycred_unpublish_posts_on_minus', 999, 3 );
function mycred_unpublish_posts_on_minus( $reply, $request, $mycred ) {
if ( $reply === false ) return;
@gabrielmerovingi
gabrielmerovingi / Rank Progress Shortcode
Last active September 16, 2016 23:08
A simple shortcode that will return either the current users or a given users rank progress using Visual Composers Progress Bar.
/**
* Users Rank Progress
* This shortcode will render the users current rank progress using the Visual Composer Progress Bar shortcode.
* Shows either the current users rank progress or a given user ids. Returns the content if user is not logged in.
* Requries myCRED 1.4
* @version 1.1
*/
add_shortcode( 'mycred_rank_progress', 'mycred_pro_users_rank_progress' );
function mycred_pro_users_rank_progress( $atts, $content = NULL ) {
// Make sure we are logged in
@gabrielmerovingi
gabrielmerovingi / Max Lottery Entries
Last active December 11, 2021 18:32
A simple example of how to adjust the myCRED Lottery add-on to add an option to set a maximum lottery entries before the lottery is closed.
/**
* Step 1 - Add new ticket preference
* In this example we will insert this under "Participation" section
* of the lottery edit screen.
* @version 1.0
*/
add_action( 'mycred_edit_lotto_participation', 'add_max_lottery_entries_settings' );
function add_max_lottery_entries_settings( $lottery ) {
$tickets = get_post_meta( $lottery->id, '_tickets', true ); ?>
@gabrielmerovingi
gabrielmerovingi / bbPress Forum Awards
Created March 18, 2014 09:22
A very basic example of using the mycred_send shortcode to allow an administrator of a bbPress forum to send points to the topic author. In this example, we send points as a thank you for helping in the support forum or points for reporting bugs.
/**
* bbPress Give Points
* Allows admins to give points to topic authors.
* @version 1.0
*/
add_action( 'bbp_theme_after_reply_content', 'mycred_pro_insert_forum_buttons' );
function mycred_pro_insert_forum_buttons() {
// Get Reply Author ID
$reply_author_id = bbp_get_reply_author_id( bbp_get_reply_id() );
@gabrielmerovingi
gabrielmerovingi / Show Users of Rank Example
Created March 19, 2014 11:02
A simple example of how you can show 5 users of each rank in your rank archive page. Remember that this code snippet goes inside the loop!
/**
* Show 5 Users of a specific rank
* This code snippet is placed inside the Loop of the archive-mycred_rank.php file
* Requires BuddyPress + myCRED 1.3+
* @version 1.0
*/
if ( function_exists( 'mycred_get_users_of_rank' ) && function_exists( 'bp_core_get_user_domain' ) ) {
$five_users = mycred_get_users_of_rank( get_the_ID(), 5 );
if ( ! empty( $five_users ) ) {
echo '<p><strong>my</strong>CRED members with this rank:</p><div class="mycred-users-of-rank">';
@gabrielmerovingi
gabrielmerovingi / Add Marker to buyCRED Purchases
Created March 28, 2014 14:27
Example of adding custom details to buyCRED purchases.
/**
* Step 1 - Create custom Shortcode
* Creates a custom buyCRED shortcode that includes
* the marker attribute that is passed on to the gateway.
* @version 1.0
*/
add_shortcode( 'mycred_custom_buy', 'mycred_render_custom_buy_shortcode' );
function mycred_render_custom_buy_shortcode( $atts, $title = 'Buy Points' ) {
if ( ! function_exists( 'mycred' ) ) return 'myCRED is not installed';
@gabrielmerovingi
gabrielmerovingi / myCRED Front End Editor
Created March 28, 2014 23:18
Example of a simple adjustment form for myCRED that can be used from the front.
add_shortcode( 'mycred_manual_adjustments', 'mycred_render_manual_adjustments' );
function mycred_render_manual_adjustments( $atts, $content = NULL ) {
// Must be logged in
if ( ! is_user_logged_in() ) return;
// myCRED must be enabled
if ( ! function_exists( 'mycred' ) ) return 'myCRED is not installed';
// Load myCRED
$mycred = mycred();
add_filter( 'mycred_add', 'enforce_mycred_form_limit', 10, 3 );
function enforce_mycred_form_limit( $reply, $request, $mycred ) {
// First we only want to target form submissions
if ( $request['ref'] != 'contact_form_submission' ) )
return $reply;
// If already declined by someone, horour it
if ( $reply !== true ) return $reply;
// Limit
@gabrielmerovingi
gabrielmerovingi / Buy Disk Space Example
Last active August 29, 2015 13:57
Custom code example on how to sell disk space updates for Multisites using WooCommerce virtual products and myCRED as a gateway. http://wordpress.org/support/topic/how-to-sell-site-upload-space
add_action( 'mycred_paid_for_woo', 'mycred_apply_purchased_disk_space', 10, 2 );
function mycred_apply_purchased_disk_space( $order, $cui ) {
if ( ! function_exists( 'get_site_option' ) ) return;
$space_bought = 0;
// Loop though all items in order to see if we bought space
$items = $order->get_items();
foreach ( $items as $item ) {
// Check for the custom field we set with the amount
@gabrielmerovingi
gabrielmerovingi / Restrict bbPress Forum Points
Created April 8, 2014 09:08
Place this code snippet in your themes functions.php file to restrict myCRED from giving points for forum actions.
/**
* Restrict bbPress Forum Points
* If a forum has the custom field "NO_MYCRED_POINTS" set to "yes"
* no points are awarded for any action taken in the forum.
* @version 1.0
*/
add_filter( 'mycred_add', 'mycred_disable_bbp_forum_points', 98, 3 );
function mycred_disable_bbp_forum_points( $reply, $request, $mycred ) {
// If already declined or bbPress is not installed, bail.
if ( $reply === false || ! function_exists( 'bbp_get_topic_forum_id' ) )