3

I want to wrap <strong></strong> around the words "logged out":

add_filter( 'gettext', 'wpse17709_gettext', 10, 2 );
function wpse17709_gettext( $custom_translation, $login_texts ) {

    // Messages
    if ( 'You are now logged out.' == $login_texts ) { return ''; } // Log out message

    return $translation
}

...however adding HTML elements to the text string breaks my page.

How can I add <strong></strong> to this message text? Is there a means other than gettext?

5

3 Answers 3

4

This allows for the message to be overwritten specifically for the loggedout message while leaving all other messages alone. Here is more documentation on the filter.

add_filter( 'wp_login_errors', 'my_logout_message' );

function my_logout_message( $errors ){

    if ( isset( $errors->errors['loggedout'] ) ){
        $errors->errors['loggedout'][0] = 'This is the <strong style="color:red;">logged out</strong> message.';
    }

    return $errors;
}
2
  • Most impressive. I wasn't aware of this means of filtering. Jan 22, 2016 at 5:36
  • 1
    Usually when using filters you want to pass a parameter to the function, to get the data you are looking to filter. When it is an object, in this case, I do not want to destroy it. I only reset a specific value that may exist in the object. Honestly, I learned something too! Great question. Jan 22, 2016 at 5:42
1

Based on Sven's recommended solution and Tom J Nowell's warning about gettext() (see comments for both), I've fashioned the following solution:

add_filter( 'login_message', 'wpse_215289_custom_logout_message' );
add_action( 'login_head','wpse_215289_custom_login_head' );

// Detect logout and add custom message.
function wpse_215289_custom_logout_message() 
{
    //check to see if it's the logout screen
    if ( isset($_GET['loggedout']) && TRUE == $_GET['loggedout'] ){
        $message = "<p class='message'>Custom log-out message.</p>";
    }
    return $message;
} 

// Remove original message via CSS.
function wpse_215289_custom_login_head() 
{
    ?>
    <style type="text/css">
    .message:nth-child(2) { display:none; }
    </style>
    <?php
}
1
  • 1
    I down voted this answer because of the solution to hide the other message with CSS. I have done it many times, but when you can output exactly what you want it is much better. If you edit the answer with your implementation you and I will both gain some points! ;) ( just 1 for me, and the down vote will go away ). Jan 22, 2016 at 5:37
1

Change “You are now logged out” text using filter login_messages

function custom_logout_message(){
  return 'You are not login!';
}
add_filter( 'login_messages', 'custom_logout_message' );
1
  • 1
    @Clarus Dignus, you can change using filter "login_messages", an I have used in my answer.
    – Billu
    Dec 26, 2017 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.