0

I'm struggling with a database issue. I took control over a solution created by someone else.

When inserting / updating users in the database, the earlier developers wrote a custom database script to update the wp_users table, and not through WordPress's API. They found out that they would use the user_nicename column for storing the first name of the user, and the display_name for storing the last name of the user. The names contains characters outside the english alphabet.

The script writes to these columns and we end up with a user_nicename like Harald Åge, which contains spaces and non-english characters. When using WordPress's API, it tries to make the user_nicename URL-friendly, but when using a database script to insert / update the user directly, WordPress won't have anything to say about this.

So now we have a partially corrupted database with non-URL-friendly user_nicenames.

My question is: Is there any way to regenerate these URL-friendly fields?

I guess it's possible to write a custom php-script which goes through all the users one by one and try to update the user_nicename value, or by letting WordPress handle that itself. Is there any other way of doing this?

And, in the future, we want to store the user's first name and last name in the wp_usermeta table instead, like WordPress does.

1

The obvious method would be to write a custom WP-CLI script to run through the table, extract the first and last names, use sanitize_title to create a new nicename and update the record. You'd also need to copy the first and last names in wp_usermeta as you mention, at the same time.

I'm not sure I'm telling you anything you don't already know here.

  • That's what I thought too. I guess this is the simplest way (and maybe the only correct way) to do this. Didn't know about sanitize_title though. That's exactly what I needed. I want to add this though: some of the users in the table does already have their user_nicename correct, due to that they've not been updated yet. I think it's a better idea to get the first and last name through the CRM integration we have and update the database with the values from that. – Anders Stensaas May 31 '16 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.