On the default WordPress login page, how do you change the label, "Username", to something else?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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' );
回答2:
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:
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