Customize Transfers in 1.5

Home / Tutorials / Customize Transfers in 1.5

Description

In this tutorial we will customize the new transfer form in 1.5 allowing your users to add a message to the transfer recipient. If you are using myCRED 1.3.x or 1.4.x, you should consult this tutorial instead. Remember that the code examples given in this tutorial goes into your theme’s functions.php file or into an appropriate file of your custom plugin.

For the complete code please consult the “The Final Code” tab above.

The transfer form was re-written in 1.5 by adding in a form element. This means that any HTML form elements inserted in the form will be submitted to the ajax script that handles the transfer action.

What it means is that we no longer need to adjust the form script as we did in previous tutorials!

The action and filter hooks used by the transfer form in previous versions are still available, so that has not changed. Only how the information inside the transfer form is sent has.

Example 1: Transfer Messages

Let your users send a short message to the transfer recipient which is then inserted into the log entry that the recipient sees in their log.

This example requires myCRED 1.5.1 or higher. If you are using myCRED 1.5, please download a fresh copy of 1.5 from wordpress.org and replace your current version via FTP for this to work.

New in 1.5

Add the Message field

First, we need to add in a new field where users will enter their message. The transfer form offers several ways to do this, but we will in this tutorial use the same action hook as in the previous tutorial and insert it under the recipient field.

/**
 * Step 1 : Insert Message Field
 * @version 1.0
 */
add_action( 'mycred_transfer_form_to', 'mycred_pro_transfer_message_field' );
function mycred_pro_transfer_message_field() {
   echo '<label>Message</label><input type="text" name="transfer-message" id="mycred-transfer-message" value="" placeholder="Optional message to recipient" />';
}

Save the Message

Next up, we will be saving the message using the new mycred_transfer_data filter which allows us to add custom details to each transfer data that is saved into the log.

/**
 * Step 2 : Save Message
 * @version 1.0
 */
add_filter( 'mycred_transfer_data', 'mycred_pro_save_transfer_message', 10, 3 );
function mycred_pro_save_transfer_message( $data, $transaction_id, $post ) {

	// Default message.
	$message = 'No message';

	// If a message is set
	if ( isset( $post['transfer-message'] ) )
		$message = sanitize_text_field( $post['transfer-message'] );

	// Add to the data array
	$data['message'] = $message;

	// return the results
	return $data;

}

Display the Message

Finally we will need to hook in and replace our custom %message% template tag with the message that got saved.

/**
 * Step 3 : Show the message
 * @version 1.0
 */
add_filter( 'mycred_parse_log_entry_transfer', 'mycred_pro_show_message_in_log', 10, 2 );
function mycred_pro_show_message_in_log( $content, $log_entry ) {

	// Unserialize data
	$data = maybe_unserialize( $log_entry->data );

	// Default message
	$message = 'No message';

	if ( isset( $data['message'] ) )
		$message = $data['message'];

	// Replace and return
	return str_replace( '%message%', $message, $content );

}

Example 2: Verify with password

In this example, we will add a password verification to transfers meaning that a user must enter their account password in order for the transfer to be approved. Entering the wrong password or no password will decline the transfer and prompt an error message.

Add the password field

Just as in the previous example, we first need to insert a new input element where the user will be entering their password.

/**
 * Step 1 : Insert Message Field
 * @version 1.0
 */
add_action( 'mycred_transfer_form_to', 'mycred_pro_transfer_message_field' );
function mycred_pro_transfer_message_field() {
 ?>
<input type="text" style="display:none;" value="" />
<label>Confirm with your password</label>
<input type="password" name="pwd" id="mycred-user-pwd" autocomplete="off" value="" placeholder="required" />
<?php
}

You will notice that i have added in an extra input field but I am hiding it via the style attribute. This is to disable the auto-fill functionality in Chrome, which will auto-fill the password field and the first input field before it even if you use autocomplete="off".

Verify Password

Next we will need to hook in to the transfer process just before it is executed and verify the password the user provided. This is done via the wp_check_password function which WordPress provides us. It will hash the provided password and compare it with the one stored in your database.

If the password is missing or incorrect, we kill the process.

/**
 * Step 2 : Verify Password
 * @version 1.0.1
 */
add_action( 'mycred_transfer_ready', 'mycred_pro_verify_transfer_pwd', 1, 4 );
function mycred_pro_verify_transfer_pwd( $post, $prefs, $transfer_addon, $type ) {

	// If the password is empty
	if ( $post['pwd'] == '' )
		die( json_encode( 'Please enter your password!' ) );

	// Get the senders user object
	$sender = wp_get_current_user();

	// Let WordPress check if the provided password is correct
	if ( ! wp_check_password( $post['pwd'], $sender->user_pass, $sender->ID ) )
		die( json_encode( 'Incorrect Password' ) );

}

The messages that we use in the above code snippet will be shown as the button label in your transfer form and if you have set to reload the page after transfer the page will also reload.

  1. Hello Master G.

    Thanks for updating this tutorial for adding a custom message to transfers on v1.5+.

    I’ve implemented the code as stated above and the transfer works fine but the custom message does not appear anywhere. It does not show in the recipient’s myCRED points account nor does it come through as a message to the recipient. I really like to implement this and I’m surprised it is not part of standard myCRED by now.

    Also, there appears to be a conflict when trying to implement both the code sections for custom message and password verification.

    1. Hey.

      Make sure you are using the latest version of myCRED with this code. You should preferably be using 1.5.2. Also, if you are using the latest version but still can’t get it to show up, make sure you have pasted the code correctly and you have removed your old code (since the two might conflict).

  2. Gabriel,
    It does not work for me either. I have done everything you stated above. But… When I export the log from myCRED panel, the messages column shows in the CSV-file. How do I get it to show in my front end history table (and the back end log)?

    Thanks in advanced!

    1. Ok. So if I understand you correctly, the message is not shown in the recipients log? Do you just see the template tags %message% or do you see the default “No message” text?

    2. Hey David.

      Did you include the %message% template tag in your log template used for transfers to show the message?

      i.e.
      %plural% transfer from %display_name% Message: %message%

  3. Hi Gabriel,

    Is this code working in version 1.6?

    I copy paste your code and just changed the custom field’s name.

    The transfer is done, and I got a log too.
    But the log doesn’t show the custom field value, instead it shows “Withdrawal of Points to PayPal:%message%”

    Please advise, thanks!

    1. Hi. Yes it should work as the Transfer add-on has received no update in 1.6. Create a pastebin and paste in the code you have in your theme’s functions.php file. Just paste in the code you pasted from here so I can check that you pasted in everything correctly.

  4. Ok, I tried it and have same results. The message is saved but not sure how to get it to display in the log template. Their is no column for it.
    Would love to get this working, if anyone has solution that didnt make it to this thread please share.

    Thanks.

  5. Hi

    I have looked through the code and how to, but i don´t understand where to put the code. What file??
    Pleae help
    another thing. i use the Email to user option once points are sent, how do I then include the message once set up. Thanks and please help me!!!

  6. Hi

    I got it to work. Just didn´t read right. But now i get emails to the admin as a notification and to the users after they transfered. I do not see the message in the email and the log entry. what can I do.ö THANKS

  7. Hello Gabriel,
    First, thank you for your amazing plugin and addons.
    I am quite interested in the verify password bit but have been unable to get it to work properly.
    The Password field does show on the form but the form is submitted without any points being sent regardless of
    the password being the correct one or not
    I am using MyCred v1.6.9
    If you could spare a minute to help it would be great.
    Thank you

    1. Try replacing the second piece of code with this:

      add_action( 'mycred_transfer_ready', 'mycred_pro_verify_transfer_pwd', 1, 5 );
      function mycred_pro_verify_transfer_pwd( $transaction_id, $post, $prefs, $transfer_addon, $type ) {
      
      	// If the password is empty
      	if ( $post['pwd'] == '' )
      		die( json_encode( 'Please enter your password!' ) );
      
      	// Get the senders user object
      	$sender = wp_get_current_user();
      
      	// Let WordPress check if the provided password is correct
      	if ( ! wp_check_password( $post['pwd'], $sender->user_pass, $sender->ID ) )
      		die( json_encode( 'Incorrect Password' ) );
      
      }
      

      The available attributes for the mycred_transfer_ready action has changed so it might be the issue.

Leave a Reply

Last edited October 28, 2016

Spin to win points 💰
Kindly get back to your previous orientation view... your wheel is rolling there...

Do you really like this awesome addon and wanna use on your site? click here to get it now 👍
myCRED Fortune Wheel special offer unlocked!
You have a chance to win free tokens. Are you feeling lucky? Give it a spin.
* You can spin the wheel only once in a month.
* You can spin again any time by paying 500 points
* Once you pay for spin you are not able to refund it.
You must login to play mycred fortune wheel
11
Happy HolidaysGET DISCOUNT