mycred_user_row_actions

Codex / mycred/balance / Filters / mycred_user_row_actions

Navigation:

  1. Description
  2. Used By
  3. Parameters
  4. Example

Description

This filter allows you to adjust myCRED row actions for the user balance column under “Users” in the admin area.

 

Used By

  • Class: myCRED_Admin Method: custom_user_column_content()

 

Parameters

  • actions (array)
    Associative array of actions. By default actions are “History” and “Adjust”.
  • user_id (int)
    User ID.
  • mycred (int)
    The myCRED_Settings Class object.

 

Example

Example 1: Add a new action called “Reset” which resets a users balance if it is higher then 0.

<?php
// Add action
add_filter( 'mycred_user_row_actions', 'my_custom_row_action', 10, 3 );
function my_custom_row_action( $actions, $user_id, $mycred )
{
	if ( $mycred->get_users_cred( $user_id ) > $mycred->format_number( 0 ) ) {
		$args = array(
			'action'  => 'reset-points',
			'user_id' => $user_id,
			'token'   => wp_create_nonce( 'reset-users-balance' )
		);

		$url = add_query_arg( $args, admin_url( 'users.php' ) );
		$actions['reset'] = '<a href="' . $url . '">' . __( 'Reset' ) . '</a>';
	}
	return $actions;
}

// Handle action
add_action( 'admin_init', 'handle_row_action' );
function handle_row_action()
{
	if ( isset( $_GET['action'] ) && isset( $_GET['token'] ) && isset( $_GET['user_id'] ) ) {
		if ( $_GET['action'] != 'reset-points' ) return;

		$mycred = mycred_get_settings();
		$cui = get_current_user_id();

		if ( !$mycred->can_edit_creds( $cui ) ) return;
		if ( wp_verify_nonce( $_GET['token'], 'reset-users-balance' ) ) {
			$balance = $mycred->get_users_cred( $_GET['user_id'] );
			$balance = $mycred->number( 0-$balance );
			$entry = __( 'Account reset by admin.' );
			$mycred->add_creds( 'reset', $_GET['user_id'], $balance, $entry, $cui );
			add_action( 'admin_notices', 'my_admin_notice' );
		}
	}
}

// Add notice if successful
function my_admin_notice() { ?>
	<div class="updated">
		<p><?php _e( 'Users account was successfully reset.' ); ?></p>
	</div>
<?php
}
?>

Last edited March 5, 2016