Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipokkel/4be7ef98e4c8ddc2562285129b954a36 to your computer and use it in GitHub Desktop.
Save ipokkel/4be7ef98e4c8ddc2562285129b954a36 to your computer and use it in GitHub Desktop.
Example of swapping a display name in the PMPro emai body.
<?php
/**
* This recipe replaces the current user's display name in an email
* with the recipient's display name.
*
* This is an example of replacing text in the email body that may
* be handy when admin is the current user that trigges a custom mail/template from
* the administrative area and the administrator's name appeared in places in the
* email due to missing variables passed to the email class from a custom email/template.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_email_body_swop_current_user_display_name( $body, $email ) {
global $current_user;
// Get user
$user = get_user_by( 'email', $email->email );
// Let's check if the current user matches the recipient
if ( $current_user->ID !== $user->ID ) {
// replace the current user's display name with the recipient's display name.
$body = str_replace( $current_user->display_name, $user->display_name, $body );
}
return $body;
}
add_filter( 'pmpro_email_body', 'my_pmpro_email_body_swop_current_user_display_name', 20, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment