• Resolved Mike Brooks

    (@mibrook)


    Hello all,
    I have been having trouble getting a function to work. I have manually added custom fields to clr_user. I want to be able to update the fields from the edit user page. I have gotten the fields and correct values to show up on the pages. I cannot get the data to update. I have two fields ‘databaseid’ (not the user_id: ties to a different app)and ‘companyid’. I realize now I should have put these in the metadata table. But, there are in the user table for now. The following is my code:

    /**addextra user fields*/
    add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
    add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
    
    if ( current_user_can( 'manage_options' ) ) {
    function my_show_extra_profile_fields( $user ) { ?>
    
    	<h3>Extra profile information</h3>
    
    	<table class="form-table">
    <tr>
    	<th><label for="databaseid"><?php _e('SSOID') ?></label></th>
    	<td><input type="text" name="databaseid" id="databaseid" value="<?php echo esc_attr($user->databaseid) ?>" class="regular-text" /></td>
    </tr>
    <tr>
    	<th><label for="companyid"><?php _e('Company ID') ?></label></th>
    	<td><input type="text" name="companyid" id="companyid" value="<?php echo esc_attr($user->companyid) ?>" class="regular-text" /></td>
    </tr>
    
    	</table>
    <?php }} else {}
    
    /**save extra user fields*/
    add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
    add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
    
    function my_save_extra_profile_fields( $user_id ) {
    
    	if ( !current_user_can( 'edit_user', $user_id ) ) {
    		wp_update_user(array('ID'=> $user_id, 'databaseid' => $_POST['databaseid']));
    		wp_update_user(array('ID'=> $user_id, 'companyid' => $_POST['companyid']));
    } else {return false;}
    }

    The last part wp_update_user is the portion that won’t work. I need the values to update upon submit. The rest of the code works as intended. I’ve looked through a ton of examples, tried dozens of variations with wp_update_user and update_metadata, but nothing will work. Again, I am not writing to the metadata table. I am writing to the user table.

Viewing 1 replies (of 1 total)
  • Thread Starter Mike Brooks

    (@mibrook)

    I figured this out for myself. I ended up removing the entries from clr_user and adding them to clr_usermeta. Then I changed a few lines of code and everything worked.
    This is the code I used:

    /**add extra user fields*/
    add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
    add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
    add_action( 'user_new_form', 'my_show_extra_profile_fields' );
    
    if ( current_user_can( 'manage_options' ) ) {
    
    function my_show_extra_profile_fields( $user ) { ?>
    
    	<h3>Extra profile information</h3>
    
    	<table class="form-table">
    
    		<tr>
    			<th><label for="databaseid">SSO-ID</label></th>
    
    			<td>
    				<input type="text" name="databaseid" id="databaseid" value="<?php echo esc_attr( get_the_author_meta( 'databaseid', $user->ID ) ); ?>" class="regular-text" /><br />
    
    			</td>
    		</tr>
    <tr>
    			<th><label for="companyid">Company-ID</label></th>
    
    			<td>
    				<input type="text" name="companyid" id="companyid" value="<?php echo esc_attr( get_the_author_meta( 'companyid', $user->ID ) ); ?>" class="regular-text" /><br />
    
    			</td>
    		</tr>
    <tr>
    			<th><label for="companyname">Company Name</label></th>
    
    			<td>
    				<input type="text" name="companyname" id="companyname" value="<?php echo esc_attr( get_the_author_meta( 'companyname', $user->ID ) ); ?>" class="regular-text" /><br />
    
    			</td>
    		</tr>
    	</table>
    <?php }} else {}
    
    /**update extra user fields*/
    add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
    add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
    add_action( 'user_new_form', 'my_save_extra_profile_fields' );
    
    function my_save_extra_profile_fields( $user_id ) {
    
    	if ( !current_user_can( 'edit_user', $user_id ) )
    		return false;
    
    	update_user_meta( $user_id, 'databaseid', $_POST['databaseid'] );
    	update_user_meta( $user_id, 'companyid', $_POST['companyid'] );
    	update_user_meta( $user_id, 'companyname', $_POST['companyname'] );

    This code adds extra fields to the edit user profile and the new user page, then ensures that the fields will update. Please note that this code is modified to only allow admins to see this data. It will not show up for any non-admin pages. All this code resides in functions.php

Viewing 1 replies (of 1 total)
  • The topic ‘Post Data to clr_user table through front end’ is closed to new replies.