Link to home
Start Free TrialLog in
Avatar of jblayney
jblayneyFlag for Canada

asked on

cant change wordpress user email after server upgrade

Hello,

I have some code that was for updating a Wordpress uses profile page. It worked fine on my old server, but now I have a new site with php 7.2 and users cannot change their email address. This code worked for many years on php 5 and old server.

elseif(email_exists(esc_attr( $_POST['email'] )) != $current_user->id )
            $error[] = __('This email is already used by another user.  try a different one.', 'profile');

whenever user try to change their email, they get the error

How do I fix this?
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

I can't see anything in your code that should break with PHP7, but your logic does look a little 'off'.

The email_exists() function will return an ID of a user if the address is already in use, but it will return false if it hasn't been used. Your logic is basically saying if the address hasn't been used, then generate an error saying it's already been used !!

The only way your code won't generate that error is if they're trying to change their email address to the one that's already registered, which doesn't make a lot of sense. It also won't generate the error if the users ID is 0, which I think is unlikely (possibly a new user!)

I'm very surprised that you say it worked for years on PHP5.
1) First, upgrade to latest WordPress. If you're not at latest 5.X then you'll have to lookup your specific version to see how your version works.

2) Change email address visiting Left Menu -> Your Profile.

3) You will see 2x messages...

Top: Your email address has not been updated yet. Please check your inbox at foo@foo.com for a confirmation email.

By Email Field: There is a pending change of your email to foo@foo.com. Cancel.

4) Latest WordPress works the same on all PHP (5.6 through 7.3.6.2) versions.
Avatar of jblayney

ASKER

Hi Guys,

thanks for responding. Everything on site is latest, wordpress, php, plugins.. I just launched it.

I should say that I inherited this code from another developer. That being said it did work on old server. ( I re-wrote 95% of old website, but I left this page as it seemed simple enough)

Chris Stanyon
i see that too, i guess the reason is, it allows users to change other form fields such as name and website, but will let the email go through like a validation as long as its doesn't change..

David Favour
I can change the users email in the dashboard as an administrator, but most users are just subscribers and this form is for them.

Maybe the solution is to just rewrite it, I tried this with no luck.

elseif(email_exists(esc_attr( $_POST['email'] )) != $current_user->user_email )
My code above doesn't make sense..

1.  It would first need to check if it is the exact same email as the current user.. in which case let it through
2. I it is different than current users email, does it exist in the system.. if it doesn't, then go through else error message
You said, "I can change the users email in the dashboard as an administrator, but most users are just subscribers and this form is for them."

Simple solution, will likely be to remove all related custom code + let WordPress handle this itself.
David Favor
But there are many custom users fields, the form needs to accommodate them
ASKER CERTIFIED SOLUTION
Avatar of jblayney
jblayney
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial