Use code "BFCM22" "BFCM22" "BFCM22" to Get 22% OFF

Customize Transfers in 1.5

Navigation:

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.

11