I'm trying to change the default label for the Wordpress lost password form which says "Username or E-mail". I need it to just read "Email" and I don't want to edit any core Wordpress files. I've already achieved this for the default login form using:
global $pagenow;
if ($pagenow==='wp-login.php') {
add_filter( 'gettext', 'user_email_login_text', 20, 3 );
function user_email_login_text( $translated_text, $text, $domain ) {
if ($translated_text === 'Username') {
echo $translated_text;
$translated_text = 'Email';
}
return $translated_text;
}
}
And this seems to also work on the other default forms for labels consisting of single words. However, if I augment the if condition to include | $translated_text === 'Username or e-mail' it doesn't work.
Any ideas? I really don't want to have to code a bespoke form as this doesn't fit in with how my system works.
Also, I wonder why Wordpress spells "Email" as "E-mail" on the lost password form, but without the dash on all other forms...
I dont know if you allready solved this. Anyway your code is allmost correct. You were only missing that the string is case sensitive and to include the colon. Like this: if ($translated_text === 'Username or E-mail:')
No need to echo $translated_text either
This works on my installation:
global $pagenow;
if ($pagenow==='wp-login.php') {
add_filter( 'gettext', 'user_email_login_text', 20, 3 );
function user_email_login_text( $translated_text, $text, $domain ) {
if ($translated_text === 'Username or E-mail:') {
$translated_text = 'Email';
}
return $translated_text;
}
}
Related
Is there a hook to change the Total title from the WooCommerce order received page's order overview? Please see the below image to understand better
Yes, you can use the gettext WordPress filter to "translate" (rename in your case) that string of text.
Your string is inside the thankyou.php WooCommerce template:
esc_html_e( 'Total:', 'woocommerce' );
Based on WooCommerce: How to Translate / Rename Any String tutorial, the right code should be the following:
add_filter( 'gettext', 'bbloomer_translate_woocommerce_strings', 9999, 3 );
function bbloomer_translate_woocommerce_strings( $translated, $untranslated, $domain ) {
if ( ! is_admin() && 'woocommerce' === $domain ) {
switch ( $translated ) {
case 'Total:':
$translated = 'Whatever:';
break;
}
}
return $translated;
}
I'm coming across an issue when trying to register a second user account once someone registers as a customer via WooCommerce. I have added the following woocommerce_created_customer hook:
add_action('woocommerce_created_customer', function($customer_id)
{
if(isset($_POST['second_user_first_name']) && isset($_POST['second_user_last_name']))
{
$createSecondUserId = wp_create_user(strtolower($_POST['second_user_first_name'].'-'.$_POST['second_user_last_name']).'-'.$customer_id, wp_generate_password(), 'test#test.com');
if(is_wp_error($createSecondUserId))
{
$errors = $createSecondUserId->errors;
print_r($errors);
die();
}
}
});
However I get the following error when submitting a new WooCommerce registration:
Array ( [existing_user_login] => Array ( [0] => Sorry, that username already exists! ) )
It's strange as I'm setting a random username within the wp_create_user function, so the usernames should not clash. Has anyone got any ideas?
You can use username_exists() to determines that the given username exists.
add_action( 'woocommerce_created_customer', function($customer_id){
if( isset( $_POST['second_user_first_name'] ) && isset( $_POST['second_user_last_name'] ) ) {
if( !username_exists( strtolower( $_POST['second_user_first_name'].'-'.$_POST['second_user_last_name'] ).'-'.$customer_id ) ){
$createSecondUserId = wp_create_user( strtolower( $_POST['second_user_first_name'].'-'.$_POST['second_user_last_name'] ).'-'.$customer_id, wp_generate_password(), 'test#test.com' );
if(is_wp_error($createSecondUserId)){
$errors = $createSecondUserId->errors;
print_r($errors);
die();
}
}
}
});
If the username already exists you can create a new one by adding a progressive numeric suffix. In this way you will be sure that the username of the second account will always be unique.
Note that if you run multiple tests with your current code you need to
make sure you remove the user with the email address test#test.com
otherwise you will get an error: [existing_user_email] => Sorry, that email address is already used!.
add_action('woocommerce_created_customer', function( $customer_id ) {
if ( isset($_POST['second_user_first_name']) && isset($_POST['second_user_last_name']) ) {
// create the username based on the form data
$username = strtolower( $_POST['second_user_first_name'] . '-' . $_POST['second_user_last_name'] ) . '-' . $customer_id;
// if the username already exists it creates a unique one
if ( username_exists($username) ) {
$i = 0;
while ( username_exists($username) ) {
$username = $username . '-' . ++$i;
}
}
// create the second user
$createSecondUserId = wp_create_user( $username, wp_generate_password(), 'test#test.com' );
}
});
The code has been tested and works. Add it to your active theme's functions.php.
How can I disable the password change option for specific user in Wordpress?
I have a user where I set a new password each week for multiple users. But some tried to change the password for this user. I don't want that - but only for this specific user profile. All the other users should be able toi change their password.
I have tried different plugins but none of them work.
Thanks a lot if you can help on this!
Add this in your function.php file
class Password_Reset_Removed
{
function __construct()
{
add_filter( 'show_password_fields', array( $this, 'disable' ) );
add_filter( 'allow_password_reset', array( $this, 'disable' ) );
}
function disable()
{
if ( is_admin() ) {
$userdata = wp_get_current_user();
$user = new WP_User($userdata->ID);
if ( !empty( $user->roles ) && is_array( $user->roles ) && $user->roles[0] == 'administrator' )
return true;
}
return false;
}
}
$pass_reset_removed = new Password_Reset_Removed();
We have just removed the fields to change password from the back-end of the WordPress. Now, some users will also try to reset the password using the Lost your password? form from the log-in page.
In order to prevent them from doing that, we will remove lost password link and disable the lost password form by adding below code
function remove_lost_your_password($text)
{
return str_replace( array('Lost your password?', 'Lost your password'), '', trim($text, '?') );
}
add_filter( 'gettext', 'remove_lost_your_password' );
function disable_reset_lost_password()
{
return false;
}
add_filter( 'allow_password_reset', 'disable_reset_lost_password');
Note - you can update userid - as per your requirements
I want that in the registration form instead of writing label "Username", I shall write the label "Artist". I have referred to question But can't get it to work. I have shown my registration form in frontend using plugin theme my login.
Here is my code
if ($pagenow=='wp-login.php') {
add_filter( 'gettext', 'user_email_login_text', 20, 3 );
function user_email_login_text( $translated_text, $text, $domain ) {
if ($translated_text == 'Username') {
$translated_text = 'Artist Name';
}
return $translated_text;
}
}
I want to change the translation value of a text when the language is the default English USA locale.
What is the correct way to accomplish this so we don't have to change the file every time we upgrade the wordpress version?
This is a nice plugin that does just that: http://wordpress.org/plugins/quick-localization/
But if you have only a few, you can also use this code:
function filter_gettext($translation, $text, $domain) {
if ( $text == 'Recent Comments' ) {
$translations = &get_translations_for_domain( $domain );
return $translations->translate( 'Something else' );
}
return $translation;
}
add_filter('gettext', 'filter_gettext', 10, 4);