Change WordPress's login label, “Username”

2020-07-23 04:22发布

On the default WordPress login page, how do you change the label, "Username", to something else?

3条回答
我欲成王,谁敢阻挡
2楼-- · 2020-07-23 04:46

Simple Solution

add_filter(  'gettext',  'register_text'  );
function register_text( $translating ) {
    $translated = str_ireplace(  'Username or Email Address',  'Your Custom Text',  $translating );
    return $translated;
}
查看更多
不美不萌又怎样
3楼-- · 2020-07-23 04:47

Recently I get into the same situation, while I need to put the translated string to change this login label and I previously try using jQuery but didn't work so I use javascript as below:

function login_script_function() {
?>
<script type="text/javascript">

window.addEventListener('DOMContentLoaded', (event) => {
    console.log('DOM fully loaded and parsed'); // to test DOM ready
    var label_user_login = document.getElementsByTagName('label')[0];
    var label_user_pass = document.getElementsByTagName('label')[1];
    label_user_login.innerText = "<?php _e('User Name', 'text-domain'); ?>";
    label_user_pass.innerText = "<?php _e('Password', 'text-domain'); ?>";
});
</script>
<?php
}
add_action( 'login_head', 'login_script_function' );

I'm using this with the custom plugin and add the string translation with e.g. Poedit or Loco Translate

查看更多
地球回转人心会变
4楼-- · 2020-07-23 04:49

I think this is a better alternative to the previous answer.

function login_function() {
    add_filter( 'gettext', 'username_change', 20, 3 );
    function username_change( $translated_text, $text, $domain ) 
    {
        if ($text === 'Username') 
        {
            $translated_text = 'customLoginName';
        }
        return $translated_text;
    }
}
add_action( 'login_head', 'login_function' );
查看更多
登录 后发表回答