Created
July 13, 2021 07:29
-
-
Save ipokkel/4be7ef98e4c8ddc2562285129b954a36 to your computer and use it in GitHub Desktop.
Example of swapping a display name in the PMPro emai body.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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