Search code examples
phpwordpressfunctionregistrationcreateuser

wordpress user registration optional fileds with php


The WordPress registration default function just have 3 options:

 <?php wp_create_user( $username, $password, $email ); ?>

If I want to add some other fileds, like age or somtehing else! with written PHP form, how can I do this?

wp_users have some columns, like user_login & user_email $ user_status. Can I add new column like age? And, if I can, how can I register it with php page?

Like function:

wp_create_user( $username, $password, $email ,$age);

If it's impossible, how can I create a new table, like wp_specialusers and access to it using PHP, like:

wpdb::query('insert something to wp_specialusers..?') or
mysql_query('insert ...')

I want like this

<?php /* Template Name: wallet */ ?>
<?php get_header(); ?>
<form action=" --->WHERE!?<--- " method="post">
AGE : <input type="text" name="age">
<button type="submit">
</form>

tnx a lot


Solution

  • You first need to use the wp_create_user( $username, $password, $email ) function like you originally have and then you need to run the wp_update_user() function afterwards to insert additional data. The additional data columns should already exist. If you want to add custom additional user data then you should use a plugin such as AdvancedCustomFields

    <?php
    $user_id = // ID of the user you just created;
    
    $website = 'http://example.com'; // additional data
    
    $user_data = wp_update_user( 
       array( 
         'ID' => $user_id,
         'user_url' => $website
       )
    );
    
    if ( is_wp_error( $user_data ) ) {
        // There was an error; possibly this user doesn't exist.
        echo 'Error.';
    } else {
        // Success!
        echo 'User profile updated.';
    }
    ?>
    

    Update: OP wants to insert additional data at the same time of creation.

    Then use wp_insert_user()

    // Extra info to add
    $website = "http://example.com";
    $nickname = "Superman";
    $description = "My description."
    
    $userdata = array(
        'user_login' =>  'login_name',
        'user_url'   =>  $website,
        'nickname'   =>  $nickname,
        'description'=>  $description,
        'user_pass'  =>  NULL // When creating an user, `user_pass` is expected.
    );
    
    $user_id = wp_insert_user( $userdata ) ;
    
    // On success.
    if ( ! is_wp_error( $user_id ) ) {
        echo "User created : ". $user_id;
    }