• Resolved Jerry

    (@gerard749)


    Hi,

    I am trying to prevent users from changing there e-mail address the same way the username can’t be changed in the profile edit screen without preventing the user from changing there display name or password.

    I have tried function.php and a plugin without success. Can this be done.

    Thank you for your help.

    Jerry

Viewing 14 replies - 1 through 14 (of 14 total)
  • Just disable the email input field the same way the username input field is disabled,so you can either manually add a disabled attribute by hard-coding the input field, the file to check is wp-admin/user-edit.php, or you can use javascript to disable it as well. In your function.php add this:

    //assuming your script.js is located in folder js/ folder of your theme
    global $pagenow;
    if ( $pagenow == 'user-edit.php' ) {
    	wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', '', '',true );
      }

    The script has to be loaded in the footer to work,then in your script.js use a simple code that will disable the input like so: document.getElementById('email').disabled = true; and you should be good.

    Thread Starter Jerry

    (@gerard749)

    Thank you for your response.

    The second part- script.js, is new to me but I can see there is documentation so in the morning I will spend time on this.

    Thank you for your help

    Jerry

    Thread Starter Jerry

    (@gerard749)

    Thank you for your time.

    This is way out of my skill level but anyway, I chose the option to use the javascript in function.php to disable the email input field.

    In my theme there is not a js/ folder. There is a scripts folder (theme/library/scripts) with 5 js files in the folder e.g. superfish.js. In Cpanel I created a js folder (theme/library/js) and in the js/ folder put a script.js file and in the file put ‘document.getElementById(’email’).disabled = true;’ alone- with nothing else and then I flushed cache.

    Any help would be appreciated.

    Jerry

    There is no need to create another js folder, you can use the folder ( library/scripts ) and create a js file and name it to your preference. So let’s assume the name you gave the file is script.js, in your theme functions.php insert this:

    global $pagenow;
    if ( $pagenow == 'user-edit.php' ) {
    	wp_enqueue_script( 'script', get_template_directory_uri() . '/library/scripts/script.js', '', '',true );
      }

    then in your script.js file insert this:

    function my_function() {
    	document.getElementById('email').disabled = true;
    }
    my_function();

    Thread Starter Jerry

    (@gerard749)

    Thank you for your help.

    When I am logged in as a user I can still change their email, display name and password but when I am logged in as administrator and go to a user’s profile page the user’s email is grayed out like the username and as administrator I can not change the user’s display name, password or user role. I get a notice- ERROR: Please enter an email.

    When I am logged in as an administrator an go to a user’s profile page it appears as it does by default, but when I am logged in as a user and go to the user’s profile page the only fields I have not removed are username, nickname, display name, email and password change.

    What I am going to do now is step by step start undoing changes (removals) I made to user’s profile page to see what I can find out. I will let you know how I make out.

    Of note, the WP plugin Prevent Email Change which I removed did the opposite- on the user’s profile page the email field was grayed out and when I tried to make changes to the other fields I would get the notice, but when logged in as admin and went to a user’s profile page the email was not grayed out and I could make changes to the other fields.

    Thank you for your time

    Jerry

    Ok I have a solution for you, to stop the error “Please enter an email”, change the script code to this:

    document.getElementById('email').readOnly = true;

    The letter O in the readOnly should be a capital letter,to ensure that users can’t change their email while they are logged in change the codes in your function.php file to this:

    global $pagenow;
    
    if ( ( $pagenow == 'user-edit.php' ) || ( ( $pagenow == 'profile.php' ) && ( !current_user_can( 'activate_plugins' ) ) ) ){
    
    	wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', '', '',true );
    }

    The reason while we are using the current_user_can check is to make sure the admin can only edit his own email or a user that has privilege to activate plugins, you can change the value to your preference, check here for all possible value the function accepts. Removing that check means no one can edit the email field including the admin. Use the above codes without the plugin and report your results..

    Ok I have a solution for you, to stop the error “Please enter an email”, change the script code to this:

    document.getElementById('email').readOnly = true;

    The letter O in the readOnly should be a capital letter,to ensure that users can’t change their email while they are logged in change the codes in your function.php file to this:

    global $pagenow;
    
    if ( ( $pagenow == 'user-edit.php' ) || ( ( $pagenow == 'profile.php' ) && ( !current_user_can( 'activate_plugins' ) ) ) ){
    
    	wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', '', '',true );
    }

    The reason while we are using the current_user_can check is to make sure the admin can only edit his own email or a user that has privilege to activate plugins, you can change the value to your preference, check here for all possible value the function accepts. Removing that check means no one can edit the email field including the admin. Use the above codes without the plugin and report your results..

    Also if you don’t like the javascript, you can use jquery, so the script can be loaded either in the header or footer, your choice.

    /* script.js */
    jQuery(document).ready(function ($) {
        $('input[name=email]').prop( 'readonly', true );
    });

    Then in your functions.php modify the enqueue_script function slightly

    wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', array('jquery') );

    Thread Starter Jerry

    (@gerard749)

    Thank you for your help, it is appreciated.

    Everything is back to default. The admin can change its own and users email and input fields and users can change their own email and input fields. I double checked function.php and script.js that the recent code was correct. Also, no user has the capability to activate_plugins and this I confirmed with the User Role Editor plugin.

    Thank you for all the time you have spent on this.

    Jerry

    Thread Starter Jerry

    (@gerard749)

    Again, thank you for your help.

    Regarding the javascript option, I again checked to make sure I put the code provided correctly in function.php and script.js.

    Of note, the changes (removals) made to the user’s profile page does not cause the error notice when a user is prevented from changing their email.

    Also, I went back and did a test by deactivating all the plugins on my site, and another plugin does not cause the error notice when a user is prevented from changing their email.

    Regarding the jQuery option, I don’t know if I did that correctly. In the script.js file I removed the javascript and replaced it with the jQuery exactly as shown above and made the change in my function.php child page. Everything is still back to default.

    Thank you for your time.

    Jerry

    The codes I gave you are working on my end.

    and made the change in my function.php child page

    Do you mean child theme,where exactly did you place the codes in the parent fucntion.php or the child’s.

    Anyway ,I did manage to put the whole stuff inside a simple plugin. Please download it here install and again report your results.

    The codes I gave you are working on my end

    and made the change in my function.php child page

    Do you mean child theme, where did you insert the codes I gave you, inside the parent’s functions.php or the child’s ?

    Anyway, I managed to insert the whole stuff inside a simple plugin. Please download it here activate and again report your results.

    Thread Starter Jerry

    (@gerard749)

    Thank you again for your help

    The plugin works fine- I checked on both the admin and user side.

    I noticed you used manage options this time instead of activate plugins, but that is something I can always change if I need to.

    With my theme I use two child pages. A function.php child page and a style.css child page. The function.php you provided in the forum were put in my themes function.php child page. I did remove the script.js file I created and the function.php you provided in the forum before uploading and activating your plugin.

    Thank you again for your time and your patience.

    Jerry

    Thanks to you too, so can you mark this issue as resolved please..

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Prevent users from changing e-mail address’ is closed to new replies.