Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 3560
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 2, 20222022-06-02T19:11:38+00:00 2022-06-02T19:11:38+00:00

php – WordPress: Add Interger to User_nicename if it Exists in Database

  • 0

[ad_1]

I have created a WordPress site where users register on the front-end and add information about themselves. On registration I am forcing the user_nicename to be name-surname. I am then automatically creating 2 custom post types that act as profile pages for the new users. I am using the user_nicename as the title of both.

The problem is obviously if two people register with the same name.

So I would like to check the database before the user_nicename is updated to check if it already exists. If it does I would like to add an integer (in sequence) to the end of the user_nicename.

For instance:

  • John-Smith
  • John-Smith-2
  • John-Smith-3
  • Etc.

The code I have tried is as follows. The exists_in_database piece is clearly where it goes wrong. Any help would be much appreciated. Thank you!

add_action( 'user_registration_after_register_user_action', 'ur_insert_username', 1, 3 );
function ur_insert_username( $valid_form_data, $form_id, $user_id ) {
global $wpdb;

$firstname = isset( $valid_form_data['first_name'] ) ? $valid_form_data['first_name']->value : '';
$lastname = isset( $valid_form_data['last_name'] ) ? $valid_form_data['last_name']->value : '';
$custom_nicename = sanitize_title_with_dashes( $firstname . '-' . $lastname);
    
$i = 1;
do {
  //Check in the database here
  $exists = exists_in_database($custom_nicename);
  if($exists) {
    $i++;
    $name = $first_name . $i;
  }

} while($exists);

$wpdb->update(
    $wpdb->users,
    ['user_nicename' => $custom_nicename],
    ['ID' => $user_id]
    );

{
$user_post_athlete = array(
        'post_title'   => $custom_nicename,
        'post_status'  => 'publish', // <- here is to publish
        'post_type'    => 'athlete', // <- change to your cpt
        'post_author'  => $user_id
    );
    
    $user_post_rivalry = array(
        'post_title'   => $custom_nicename,
        'post_status'  => 'publish', // <- here is to publish
        'post_type'    => 'rivalry', // <- change to your cpt
        'post_author'  => $user_id
    );
    
    // Insert the post into the database
    $post_id = wp_insert_post( $user_post_athlete );
    $post_id = wp_insert_post( $user_post_rivalry );
    }
}

[ad_2]

  • 0 0 Answers
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 8143 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1925 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1960 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.