1

I want to save the custom field values in the wp-users table. Do I have to create the fields(client and user_groups) in the table or it gets created automatically when the data gets saved into the database?

Here is my code

function custom_user_profile_fields($user){
global $wpdb;
    if(current_user_can('editor')){
        if(is_object($user) && isset($user->ID)){
            $client = get_user_meta( $user->ID, 'client', true );
            $user_groups = get_user_meta( $user->ID, 'user_groups', true );
        }else{
            $client = null;
            $user_groups = null;
        }
        ?>
        <table class="form-table">
            <tr>
                <th><label for="company">Client Name</label></th>
                <td>
                    <input type="text" class="regular-text" name="client" value="<?php echo $client; ?>" id="client" /><br />
                </td>
            </tr>
            <tr>
                <th><label for="company">User groups</label></th>
                <td><?php 
$sql = $wpdb->get_results("SELECT usergroup_name FROM `wp_usergroup`");
?>
    <select name="user_groups">
        <option>None</option>
    <?php 
    foreach($sql as $sl) { ?>
        <option value="<?php echo $sl->usergroup_name;?>"><?php echo ucfirst($sl->usergroup_name); ?></option>
    <?php
}
?>
</select></td>
            </tr>
        </table>
    <?php
    }
}
add_action( 'show_user_profile', 'custom_user_profile_fields' );
add_action( 'edit_user_profile', 'custom_user_profile_fields' );
add_action( "user_new_form", "custom_user_profile_fields" );

function save_custom_user_profile_fields($user_id){
    # again do this only if you can
    if(!current_user_can('manage_options'))
        return false;

    # save my custom field
    if( isset($_POST['client']) ) {
        update_user_meta( $user_id, 'client', sanitize_text_field( $_POST['client'] ) );
    } else {
        //Delete the company field if $_POST['company'] is not set
        delete_user_meta( $user_id, 'client', $meta_value );
    }
    if( isset($_POST['user_groups']) ) {
        update_user_meta( $user_id, 'user_groups', sanitize_text_field( $_POST['user_groups'] ) );
    } else {
        //Delete the company field if $_POST['company'] is not set
        delete_user_meta( $user_id, 'user_groups', $meta_value );
    }
}
add_action('user_register', 'save_custom_user_profile_fields');
add_action( 'personal_options_update', 'save_custom_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_custom_user_profile_fields' );

Thanks in advance for help

1 Answer 1

1

So you don't need to create those fields, that why they are called Meta fields because they don't require any setup for them.

I've improved the code and made sure it was working, applied a few good practices here and there, and added a few comments.

function custom_user_profile_fields( $user = null ) {
    global $wpdb;

    // This will check if we are dealing with the New User Page
    if ( $user instanceof WP_User ){
        // It's better to Just leave if the person doenst have permission
        if ( ! $user->exists() || ! current_user_can( 'edit_users', $user->ID ) ) {
            return;
        }
    } elseif ( ! current_user_can( 'edit_users' ) ) {
        return;
    }

    $meta = (object) array(
        'client' => '',
        'group' => '',
    );

    if ( $user instanceof WP_User ) {
        // Fetch the meta
        $meta->client = get_user_meta( $user->ID, 'client', true );
        $meta->group = get_user_meta( $user->ID, 'user_groups', true );
    }

    // Don't try to get info in the middle of the HTML (makes it harder to read the code later on)
    $usergroups = $wpdb->get_results( 'SELECT usergroup_name FROM wp_usergroup;' );

    ?>
    <table class="form-table">
    <tr>
        <th><label for="company">Client Name</label></th>
        <td>
            <input type="text" class="regular-text" name="client" value="<?php echo esc_attr( $meta->client ); ?>" id="client" /><br />
        </td>
    </tr>
    <tr>
        <th><label for="company">User groups</label></th>
        <td>
        <select name="user_groups">
        <option>None</option>
            <?php foreach ( $usergroups as $group ) { ?>
                <option <?php selected( $meta->group, $group ); ?> value="<?php echo esc_attr( $group->usergroup_name );?>"><?php echo esc_html( ucfirst( $group->usergroup_name ) ); ?></option>
            <?php } ?>
        </select>
        </td>
    </tr>
    </table>
    <?php
}

add_action( 'show_user_profile', 'custom_user_profile_fields' );
add_action( 'edit_user_profile', 'custom_user_profile_fields' );
add_action( 'user_new_form', 'custom_user_profile_fields' );

function save_custom_user_profile_fields( $user_id ) {
    // again do this only if you can
    if ( ! current_user_can( 'edit_users', $user_id ) ) {
        return;
    }

    if ( isset( $_POST['client'] ) ) {
        update_user_meta( $user_id, 'client', sanitize_text_field( $_POST['client'] ) );
    } else {
        delete_user_meta( $user_id, 'client' );
    }

    if ( isset( $_POST['user_groups'] ) ) {
        update_user_meta( $user_id, 'user_groups', sanitize_text_field( $_POST['user_groups'] ) );
    } else {
        delete_user_meta( $user_id, 'user_groups' );
    }
}
add_action( 'user_register', 'save_custom_user_profile_fields' );
add_action( 'personal_options_update', 'save_custom_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_custom_user_profile_fields' );
4
  • But I want to store both the fields in the wp-users table
    – 1991wp
    Commented Dec 14, 2015 at 8:45
  • Hi thanks for the reply , my data is not getting saved in the wp_postmeta table also
    – 1991wp
    Commented Dec 14, 2015 at 9:19
  • I did some modifications and now my data is getting stored. Can you please tell me in which table does it stores the data? wp_postmeta? and what if I want to store it in the wp_users table?
    – 1991wp
    Commented Dec 14, 2015 at 9:47
  • I just only now saw your comments, my bad. So it will not be stored at wp_postmeta it will be at wp_usermeta, which is the same type of Table as wp_postmeta but to store User Meta information instead of Posts. Check the link about how metadata works; codex.wordpress.org/Metadata_API
    – Bordoni
    Commented Dec 20, 2015 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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