Wordpress - How to change the wp-login.php page title?

It looks like it's not easily accessible as it's displayed as src:

<title><?php echo get_bloginfo( 'name', 'display' ) . $separator . $title; ?></title>

where the separator is:

    $separator = is_rtl() ? ' &rsaquo; ' : ' &lsaquo; ';

and the $title part comes from:

login_header( $title = 'Some title' , ... );

But it looks like you've already checked this out, as I see you've filed a ticket #40812 for an extra filter to change the separator.

A workaround that comes to mind, to change the separator, would be to use output buffering hacks to replace it.


The problem

As @birgire pointed out, changing the title text on wp-login.php is not easily doable, since we don't have the various title altering filters like the front end offers.

A solution

However, we can detect if we're on wp-login.php and then determine which action the user is taking: logging in, registering, or resetting their password.

The <title> tag on wp-login.php is made up of three parts:

<title><?php echo get_bloginfo( 'name', 'display' ) . $separator . $title; ?></title>

We can alter the get_bloginfo( 'name', 'display' ) and $title areas but unfortunately, $separator can not be changed with the technique outlined here.

The option_{option_name} filter ( option_blogname in this case ) can be used to modify the get_bloginfo( 'name', 'display' ) part of the title.

The $title, is passed to __() which means that we can intercept it and change it using the gettext filter.

$title is assigned __('Log In'), __('Registration Form'), and __('Lost Password') on the log in, register, and lost password pages respectively.

The code

This code will wire up the appropriate filters on wp-login.php for both of the changeable parts of the title.

/**
 * Detect if we're on wp-login.php and wire up the appropriate filters
 * based on what action being taken by the user.
 * idea via https://wordpress.stackexchange.com/a/12865/2807
 */
add_action( 'init', 'wpse_login_register_password_title' );
function wpse_login_register_password_title() {

    if ( isset( $GLOBALS['pagenow'] ) && $GLOBALS['pagenow'] === 'wp-login.php' ) {

        // Registration
        if ( ! empty( $_REQUEST['action'] ) && $_REQUEST['action'] === 'register' ) {
            add_filter( 'option_blogname', 'wpse_login_page_register_blogname', 10, 1 );
            add_filter( 'gettext', 'wpse_login_page_register_title', 10, 3 );
        }

        // Password
        else if ( ! empty( $_REQUEST['action'] ) && $_REQUEST['action'] === 'lostpassword' ) {
            add_filter( 'option_blogname', 'wpse_login_page_password_blogname', 10, 1 );
            add_filter( 'gettext', 'wpse_login_page_password_title', 10, 3 );
        }

        // Log in
        else {
            add_filter( 'option_blogname', 'wpse_login_page_blogname', 10, 1 );
            add_filter( 'gettext', 'wpse_login_page_title', 10, 3 );
        }
    }
}

Here are the filters that will modify the blogname portion of the title tag for each of the wp-login.php actions.

/**
 * Change get_bloginfo( 'name', 'display' ) portion of the <title>'s
 * text on the wp-login.php page.
 * Immediately remove the filters so that they only run once.
 */
function wpse_login_page_blogname( $value ) {
    // Log in
    remove_filter( 'option_blogname', 'wpse_login_page_blogname', 10, 1 );
    return 'This is the changed blog name for the login page.';
}

function wpse_login_page_register_blogname( $value ) {
    // Register
    remove_filter( 'option_blogname', 'wpse_login_page_register_blogname', 10, 1 );
    return 'This is the changed blog name for the register page.';
}

function wpse_login_page_password_blogname( $value ) {
    // Reset password
    remove_filter( 'option_blogname', 'wpse_login_page_password_blogname', 10, 1 );
    return 'This is the changed blog name for the password reset page.';
}

Finally, these are the filters that will modify the $title portion of the title tag for each of the wp-login.php actions.

/**
 * Translate the $title portion of the <title>'s text on the wp-login.php page.
 * Immediately remove the filters so that they only run once.
 *
 * @param string $translation  Translated text.
 * @param string $text         Text to translate.
 * @param string $domain       Text domain. Unique identifier for retrieving translated strings.
 *
 * @return string
 */
function wpse_login_page_title( $translation, $text, $domain ) {
    // Log in
    // The 'default' text domain is reserved for the WP core.
    if ( 'default' === $domain && 'Log In' === $text ) {
        $translation = 'This is the changed "Log In" text.';
        remove_filter( 'gettext', 'wpse_login_page_title', 10, 3 );
    }
    return $translation;
}

function wpse_login_page_register_title( $translation, $text, $domain ) {
    // Register
    if ( 'default' === $domain && 'Registration Form' === $text ) {
        $translation = 'This is the changed "Registration Form" text.';
        remove_filter( 'gettext', 'wpse_login_page_register_title', 10, 3 );
    }
    return $translation;
}

function wpse_login_page_password_title( $translation, $text, $domain ) {
    // Reset password
    if ( 'default' === $domain && 'Lost Password' === $text ) {
        $translation = 'This is the changed "Lost Password" text.';
        remove_filter( 'gettext', 'wpse_login_page_password_title', 10, 3 );
    }
    return $translation;
}

You can use this code in your themes functions.php

function custom_login_title( $login_title ) {
return str_replace(array( ' &lsaquo;', ' &#8212; WordPress'), array( ' &bull;', ' what ever you want'),$login_title );
}
add_filter( 'login_title', 'custom_login_title' );

This will change the login.php <title> to Log In • Blog Name what every you want

You can do the same for all admin pages but it would be $admin_title instead of $login_title

Tags:

Php

Login

Filters