The following snippet shows how to combine the Transfers add-on points transfer functionality with the Leaderboards add-on adding a new column with the [gamipress_points_transfer] shortcode to let your users transfer their points to users listed on the leaderboard.
Note: If you want to add more columns to the leaderboard, check this code snippet that will help you to add a custom column with an user custom field.
function my_prefix_leaderboard_points_transfer_column( $columns, $leaderboard_id, $leaderboard ) {
// You can limit the custom column to just a specific leaderboard
if( $leaderboard_id !== 123 ) {
return $columns;
}
// Add the new column
// The key "points_transfer" will be used to filter the column output in next function
// The "Points Transfer" will be the column title
$columns['points_transfer'] = __( 'Points Transfer' );
return $columns;
}
add_filter( 'gamipress_leaderboards_leaderboard_columns_info', 'my_prefix_leaderboard_points_transfer_column', 10, 3 );
function my_prefix_leaderboard_points_transfer_column_output( $output, $leaderboard_id, $position, $item, $column_name, $leaderboard_table ) {
// Set the points amount and the points type slug you want
$points = 100;
$points_type = 'points-type-slug';
// Set as column output the [gamipress_points_transfer] shortcode
$output = gamipress_do_shortcode( 'gamipress_points_transfer', array(
'transfer_type' => 'fixed',
'amount' => $points,
'points_type' => $points_type,
'recipient_id' => $item['user_id'],
'select_recipient' => 'no',
'button_text' => __( 'Transfer Points' )
) );
// You can get the same result with WordPress do_shortcode() function, gamipress_do_shortcode() is a developer oriented function
//$output = do_shortcode( '[gamipress_points_transfer transfer_type="fixed" amount="' . $points . '" points_type="' . $points_type . '" recipient_id="' . $item['user_id'] . '" select_recipient="no" button_text="Transfer Points"]' );
// Return the output
return $output;
}
// This filter dynamically changes based on the column key following the next pattern: gamipress_leaderboards_leaderboard_column_{key}
// In previous function the new column key is "points_transfer", so the filter will be: gamipress_leaderboards_leaderboard_column_points_transfer
add_filter( 'gamipress_leaderboards_leaderboard_column_points_transfer', 'my_prefix_leaderboard_points_transfer_column_output', 10, 6 );
// Probably a desired workflow is to refresh the leaderboard page instead of redirect the user to the transfers history
// so let's to force transfer refresh the page instead of redirect
function my_prefix_force_transfers_refresh_page( $response, $transfer, $transfer_items ) {
$referrer = isset( $_POST['referrer'] ) ? $_POST['referrer'] : '';
if( ! empty( $referrer ) ) {
$response['redirect'] = true;
$response['redirect_url'] = $referrer; // Set as redirect URL the same page where form has been submitted
}
return $response;
}
add_filter( 'gamipress_transfers_process_transfer_response', 'my_prefix_force_transfers_refresh_page', 10, 3 );