Skip to content

Instantly share code, notes, and snippets.

@gabrielmerovingi
Created March 28, 2014 23:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gabrielmerovingi/9844968 to your computer and use it in GitHub Desktop.
Save gabrielmerovingi/9844968 to your computer and use it in GitHub Desktop.
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();
// Get Editors ID
$user_id = get_current_user_id();
// Check capability right off the bat
if ( ! $mycred->can_edit_creds( $user_id ) ) return 'You are note allowed to use this feature';
ob_start();
// First we handle any request we might have
if ( isset( $_REQUEST['mycred_adjust'] ) && wp_verify_nonce( $_REQUEST['mycred_adjust']['token'], 'mycred-manual-adjustment' . $user_id ) ) {
// Get the user
$email_address = sanitize_text_field( $_REQUEST['mycred_adjust']['email'] );
$user = get_user_by( 'email', $email_address );
if ( ! isset( $user->ID ) )
echo '<p style="color:red;">User not found.</p>';
// Make sure a log description has been given
$log_entry = sanitize_text_field( $_REQUEST['mycred_adjust']['entry'] );
if ( empty( $log_entry ) )
echo '<p style="color:red;">You must fill out a log entry.</p>';
// Make sure the amount is not zero
$amount = $mycred->number( $_REQUEST['mycred_adjust']['amount'] );
if ( $amount == $mycred->zero() )
echo '<p style="color:red;">The amount can not be zero or.</p>';
// If all is well, lets adjust
if ( isset( $user->ID ) && ! empty( $log_entry ) && $amount > $mycred->zero() ) {
$mycred->add_creds(
'manual_adjustment',
$user->ID,
$amount,
$log_entry,
$user_id,
'',
'mycred_default'
);
echo '<p style="color:green;">Adjustment Complete</p>';
}
} ?>
<p>Adjust points</p>
<form action="" method="post">
<input type="hidden" name="mycred_adjust[token]" class="form-control" value="<?php echo wp_create_nonce( 'mycred-manual-adjustment' . $user_id ); ?>" />
<p><strong>Email Address</strong><br /><input type="text" class="form-control" name="mycred_adjust[email]" size="25" value="" /></p>
<p><strong>Points</strong><br /><input type="text" class="form-control" name="mycred_adjust[amount]" size="8" value="" /></p>
<p><strong>Log Entry</strong><br /><input type="text" class="form-control" name="mycred_adjust[entry]" size="25" value="" /></p>
<p><input type="submit" value="Adjust Users Balance" class="btn btn-primary" /></p>
</form>
<?php
$content = ob_get_contents();
ob_end_clean();
return $content;
}
@vincentsolutions
Copy link

Hey, I don't know if you have any time to answer this but, I found this snippet very useful ! What would be the best way to implement it ? As a standalone plugin ? As a snippet using a plugin ? Or by making a php file in my mycred folder ?

If you have any time to answer this I'd really appreciate it ! Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment