正如@birgire所指出的问题,在wp-login.php 很难做到这一点,因为我们没有像前端产品那样的各种标题更改过滤器。
但是,我们可以检测到wp-login.php 然后确定用户正在执行的操作:登录、注册或重置密码。
这个<title> 标记上wp-login.php 由三部分组成:
<title><?php echo get_bloginfo( \'name\', \'display\' ) . $separator . $title; ?></title>
我们可以改变
get_bloginfo( \'name\', \'display\' ) 和
$title 但不幸的是,
$separator 无法使用此处概述的技术进行更改。
这个option_{option_name} 过滤器(option_blogname 在这种情况下),可用于修改get_bloginfo( \'name\', \'display\' ) 标题的一部分。
这个$title, 已传递给__() 这意味着我们可以拦截它并使用gettext 滤器
$title 已分配__(\'Log In\'), __(\'Registration Form\'), 和__(\'Lost Password\') 分别在登录、注册和丢失密码页面上。
该代码将连接上相应的过滤器wp-login.php 对于标题的两个可更改部分。
/**
* 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 );
}
}
}
以下是将修改每个
wp-login.php 行动。
/**
* 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.\';
}
最后,这些过滤器将修改
$title 每个
wp-login.php 行动。
/**
* 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;
}