Change field label text on Woocommerce login form

Viewed 4377

This Question is similar to Rename username label on woocommerce login page

I am trying to change the Label "Username or email address" to "Your registered email address"

See screenshot where I am trying to change the label

I am trying to do this without modifying the form-login.php template file.

So far I tried the below code:

function wppb_change_text_login( $translated_text, $text ) {
    //Login page
    $original_text = 'Username or email address';
    $new_text = 'Your registered email address';
    if ( $text == $original_text ) {
        $translated_text = $new_text;
    }
}
add_filter( 'gettext', 'wppb_change_text_login', 30, 2 );

However, I understand that gettext is very resource intensive function and I was wondering what changes should I make to make these changes be restricted to my-account page.

2 Answers

Your code will work if you add return $translated_text; at the end in your function as in any filter hook it's mandatory to return the filtered value.

To restrict this function to my account registering form you will use the following conditions:

add_filter( 'gettext', 'wppb_change_text_login', 10, 3 );
function wppb_change_text_login( $translated_text, $text, $domain ) {
    // Only on my account registering form
    if ( ! is_user_logged_in() && is_account_page() ) {
        $original_text = 'Username or email address';

        if ( $text === $original_text )
            $translated_text = esc_html__('Your registered email address', $domain );
    }
    return $translated_text;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

enter image description here

For rename Label "Username or email address" to "Your registered email address" Please add following function into your theme function.php

add_filter(  'gettext',  'register_text'  );
add_filter(  'ngettext',  'register_text'  );
function register_text( $translated ) {
     $translated = str_ireplace(  'Username or Email Address',  'Your registered email address',  $translated );
     return $translated;
}

I have tested the above function with storefront theme and its working for me please check the screenshot https://prnt.sc/m25hwz

Related