Using Filters to Extend Restrict Content Pro Email Notices

I built a membership site using Restrict Content Pro a few months back. I loved how easy it was to set up and customize. One of the great features of the plugin is its extensibility. Pippin has done a great job of including hooks and filters within the plugin so that plugin developers can add their own functionality without needing to the edit the plugin’s core files.

The client came back to me a couple days ago and wanted to add a couple new features to the new member registration process. First of all, they wanted the new registration notifications to go to two emails. By default the plugin uses only the admin email for notifications. Secondly, they wanted the custom registration fields to be included in their notification email.

Thankfully the plugin’s extensibility made both of these requests somewhat painless to implement.

Add Another Email Address to the User Sign Up Notification

By default, the plugin uses the admin email for new user registration notifications. Looking through the code that sent the notification email, I noticed that the admin email had a filter around it I could use. The code in the plugin looked like this:

$admin_emails = array();
$admin_emails[] = get_option('admin_email');
$admin_emails = apply_filters( 'rcp_admin_notice_emails', $admin_emails );

By wrapping the $admin_emails array with apply_filters, it allowed me to edit the contents of the array before it was sent to the wp_mail function that sent out the notifications. I wrote a function that appended another email to the end of the $admin_emails array, then returned it back to the original function. Notice that I used the add_filter function to fire my function when the rcp_admin_notice_emails filter was called.

function endo_add_admin_email( $admin_emails ) {
     $admin_emails[] = '';
     return $admin_emails;
add_filter( 'rcp_admin_notice_emails', 'endo_add_admin_email' );

Now when the wp_mail function runs, it will send the notification email to both the admin email and the email specified in the endo_add_admin_email function.

Add Custom User Fields to the Notification Email

The registration form included some custom registration fields that the user had to fill out. You can learn how to add those here. The form already saved them to the database, but I also needed the data to show up in the notification email.

Using the rcp_before_admin_email_free_thanks filter, I was able to edit the content of the email before it was sent. The extra fields needed in the email were phone, city, and state. Again, I used the add_filter function to hook into the plugin, but I had to change it up because I needed two arguments from the function, both the $admin_message and the $user_id.

Here is the code in the plugin:

$admin_message = apply_filters('rcp_before_admin_email_free_thanks', $admin_message, $user_id);

In order to gain access to the third parameter ($user_id), I had to pass a fourth parameter to the add_filter function. The fourth parameter sets the number of arguments the function accepts. In this case I needed access to two, $admin_message and $user_id.

So my add_filter function looked like this:

add_filter( 'rcp_before_admin_email_free_thanks', 'endo_add_user_details_email', 10, 2 );

Once I had access to the correct data, I used the function get_user_meta to save my custom registration fields to variables. Then I appended those values to the email message and returned it to the function. The final code I used was this:

function endo_add_user_details_email( $admin_message, $user_id ) {

	$user_info = get_userdata($user_id);
	$phone = get_user_meta( $user_id, 'rcp_phone', true );
	$city = get_user_meta( $user_id, 'rcp_city', true );
	$state = get_user_meta( $user_id, 'rcp_state', true );
	$output = "";
	$output .= $admin_message . "\n\n";
	$output .= 'User Details ' . "\n\n";
	$output .= 'Name: ' . $user_info->display_name . "\n\n";
	$output .= 'Phone: ' . $phone . "\n\n";
	$output .= 'City: ' . $city . "\n\n";
	$output .= 'State: ' . $state . "\n\n";
	return $output;
add_filter( 'rcp_before_admin_email_free_thanks', 'endo_add_user_details_email', 10, 2 );

I hope this helps you with writing your own extensions for Restrict Content Pro, and any other plugins that take advantage of the extensibility of the WordPress Plugin API. To learn more about how hooks and filters work, I would recommend reading the following articles.

Writing Extensible Plugins With Actions and Filters

Inside WordPress Actions And Filters

Leave a Reply

Your email address will not be published. Required fields are marked *