Home > front end >  Custom WordPress email system
Custom WordPress email system

Time:03-23

From a custom function to register users, I inserted this function to generate and overwrite the WordPress system emails.

function custom_wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {
  global $wpcargo;
    $user_url = stripslashes( $user->user_url );
    $user_login = stripslashes( $user->user_login );
    $user_email = stripslashes( $user->user_email );
    $user_firstname = stripslashes( $user->user_firstname );
    $user_last_name = stripslashes( $user->user_last_name );
    $user_pass = stripslashes( $user->user_pass );
    $message = file_get_contents('../email/mail-template.php');
    $wp_new_user_notification_email['subject'] = sprintf( '[%s] Welcome.', $blogname );
    $wp_new_user_notification_email['headers'] = array('Content-Type: text/html; charset=UTF-8');
    $wp_new_user_notification_email['message'] = $message; 
    return $wp_new_user_notification_email;
}
add_filter( 'wp_new_user_notification_email', 'custom_wp_new_user_notification_email', 10, 3 );

Emails are being sent but I cannot get user data printed in the PHP / HTML file.

<p style="margin-top:0;margin-bottom:12px;"><b>Name</b>: <?php $user_firstname; ?></p>

I'm wrong with the code, any suggestions?

CodePudding user response:

I would use locate_template for the email contents and output buffering rather than file_get_contents - also as Zak pointed out, you have to echo your output otherwise it won't show up.

function custom_wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {
    global $wpcargo;
    $user_url = stripslashes( $user->user_url );
    $user_login = stripslashes( $user->user_login );
    $user_email = stripslashes( $user->user_email );
    $user_firstname = stripslashes( $user->user_firstname );
    $user_last_name = stripslashes( $user->user_last_name );
    $user_pass = stripslashes( $user->user_pass );
    ob_start();
    include( locate_template( '/email/mail-template.php' ); // This path may vary depending on your setup.
    $wp_new_user_notification_email['message'] = ob_get_clean();
    $wp_new_user_notification_email['subject'] = sprintf( '[%s] Welcome.', $blogname );
    $wp_new_user_notification_email['headers'] = array('Content-Type: text/html; charset=UTF-8');
    return $wp_new_user_notification_email;
}
add_filter( 'wp_new_user_notification_email', 'custom_wp_new_user_notification_email', 10, 3 );

Within your template you want to make sure you echo and esc_html() for anything that is being outputted.

<?php echo esc_html( $user_firstname ); ?> 

CodePudding user response:

PHP has a funny way of letting you just place a variable into the page even if you're not doing anything with it .. without giving you an error or even a warning.

But .. You are just placing the variable into the page code WITHOUT DOING ANYTHING... You either need to print or echo it to the page...

 <?php echo $user_firstname; ?>

Or

<?= $user_firstname; ?>
  • Related