PHP / HTML Email help.. Can't put an a href tag in content

Hello,

I am trying to send an email to the user when they reset their pw. In the email I would like for there to be a link they click. I am not able to get the a href tag in the email to work properly.

$message = __('Someone requested that the password be reset for your account.') . "\r\n\r\n";
	//$message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
	$message .= network_home_url( '/' ) . "\r\n\r\n";
	$message .= __('If this was a mistake, ignore this email and your password will remain the same.') . "\r\n\r\n";
	$message .= __('To reset your password, please click on the below link:') . "\r\n\r\n";
	$message .= '<a href="' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . '">Reset My Password</a>';

How come this is not working? Is there a better way to do this?

Any help is greatly appreciated!

Thanks,
Judah

  • Michael Bissett

    Hey @Judah, hope you're doing well this evening! :slight_smile:

    How is the a href tag not working properly, could you elaborate on that?

    And what if you were to try using this instead:

    $message = __('Someone requested that the password be reset for your account.') . "\r\n\r\n";
    	//$message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
    	$message .= network_home_url( '/' ) . "\r\n\r\n";
    	$message .= __('If this was a mistake, ignore this email and your password will remain the same.') . "\r\n\r\n";
    	$message .= __('To reset your password, please click on the below link:') . "\r\n\r\n";
    	$message .= '<a href="' . network_site_url( '/wp-login.php?action=rp&key=' . rawurlencode($user_login), 'login') . '">Reset My Password</a>';

    Please advise,
    Michael

  • Judah

    Hey @Michael,

    Sorry for the lack of description in the original post.

    I meant to say that the anchor text is not populating properly. Here is a screenshot of what the email looks like.

    Also, your code is not working as it does not generate the key in the URL when the user clicks on the link.

    Is there a way I can generate the a href tag anchor text via php emails? (I am pretty sure that I am setting up the allowing emails in html correctly)

  • Michael Bissett

    Hey @Judah,

    Ah, so you're working inside of wp-login.php? :slight_smile:

    In that case, a better way to manage what you're looking to do would be to use a mu-plugin, as the changes provided by that won't be wiped out on an update to WordPress. I'd want to revert the edits made to that file, as we'll being applying a more future-safe method. :slight_smile:

    (I believe that you're already aware of a how a mu-plugin is set up, but for anyone reading this later, here's where you can learn more on that)

    In looking at the code that WordPress uses to generate that URL by default, the "a href" isn't necessary, the line in question looks like this:

    $message .= '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . ">\r\n";

    Now, as for the mu-plugin to handle what you're looking to do... credit must be given where it is due, and this is no different:

    http://wordpress.stackexchange.com/questions/160456/how-to-apply-the-retrieve-password-message-filter/161001#161001

    Thanks to that code, here's the code you'll need to change the password reset email message:

    <?php
    
    add_filter( 'retrieve_password_message', 'wpse_retrieve_password_message', 10, 2 );
    function wpse_retrieve_password_message( $message, $key ){
        $user_data = '';
        // If no value is posted, return false
        if( ! isset( $_POST['user_login'] )  ){
                return '';
        }
        // Fetch user information from user_login
        if ( strpos( $_POST['user_login'], '@' ) ) {
    
            $user_data = get_user_by( 'email', trim( $_POST['user_login'] ) );
        } else {
            $login = trim($_POST['user_login']);
            $user_data = get_user_by('login', $login);
        }
        if( ! $user_data  ){
            return '';
        }
        $user_login = $user_data->user_login;
        $user_email = $user_data->user_email;
        // Setting up message for retrieve password
        $message = __('Someone requested that the password be reset for your account.') . "\r\n\r\n";
    	$message .= network_home_url( '/' ) . "\r\n\r\n";
    	$message .= __('If this was a mistake, just ignore this email and nothing will happen.') . "\r\n\r\n";
    	$message .= __('To reset your password, visit the following address:') . "\r\n\r\n";
    	$message .= '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . ">\r\n";
        // Return completed message for retrieve password
        return $message;
    }

    Hope this helps! :slight_smile:

    Kind Regards,
    Michael

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.