Adding a new email template tag

Restrict Content Pro has a list of template tags that can be used to dynamically insert variables into your emails. A list of the default tags can be found in  the article on email settings.

Creating a new template tag is a simple matter of registering the tag and providing a callback function for retrieving the data.

Registering your email tag

This is done with the rcp_email_template_tags filter:

function ag_rcp_email_template_tags( $email_tags ) {
    $email_tags[] = array(
        'tag'         => 'my_custom_tag',
        'description' => __( 'Description of my custom tag.' ),
        'function'    => 'ag_rcp_my_custom_tag_callback_function'
    );

    return $email_tags;
}

add_filter( 'rcp_email_template_tags', 'ag_rcp_email_template_tags' );

The tag array has three key/value pairs:

  1. tag - The actual tag for use in the email (without the % symbols). In the above example, the tag would be used like this: %my_custom_tag%
  2. description - Text description. This is displayed in the tag list on the settings page for your reference.
  3. function - The name of your callback function. We'll be creating this in the next step.

Once the above code has been added, your tag should appear in Restrict > Settings > Emails in the list of template tags:

Creating your callback function

Finally, you need to create the function listed above. This is where you retrieve and return the data you want to display with the tag. Three parameters are passed to the function:

  1. $user_id - The ID of the user receiving the email.
  2. $payment_id - The ID of the latest payment made by the user.
  3. $tag - Name of your tag

Here's what a sample function might look like for returning a piece of user meta:

function ag_rcp_my_custom_tag_callback_function( $user_id = 0, $payment_id = 0, $tag ) {
    $my_user_meta = get_user_meta( $user_id, 'my_custom_user_meta', true );

    return $my_user_meta;
}

Your function name should match what you entered for 'function' in the above array.