How To Add Custom Fields in User Profile wordpress

How To Add Custom Fields in User Profile wordpress
121 Views
<?php
add_action( 'show_user_profile', 'cxc_extra_user_profile_fields' );
add_action( 'edit_user_profile', 'cxc_extra_user_profile_fields' );

function cxc_extra_user_profile_fields( $user ) {
	$address = get_user_meta( $user->ID , 'address', true );
	$city = get_user_meta( $user->ID, 'city', true );
	$postalcode = get_user_meta( $user->ID , 'postalcode', true );
	$user_profile = ( $user_profile = get_user_meta( $user->ID, 'user_profile', true ) ) ? $user_profile : 'yes';
	?>

	<h3><?php _e( "Extra profile information", "cxc-codexcoach" ); ?></h3>
	<table class="form-table">
		<tr>
			<th><label for="address"><?php _e("Address"); ?></label></th>
			<td>
				<input type="text" name="address" id="address" value="<?php echo $address; ?>" class="regular-text" /><br />
				<span class="description"><?php _e("Please enter your address."); ?></span>
			</td>
		</tr>
		<tr>
			<th><label for="city"><?php _e("City"); ?></label></th>
			<td>
				<input type="text" name="city" id="city" value="<?php echo $city; ?>" class="regular-text" /><br />
				<span class="description"><?php _e("Please enter your city."); ?></span>
			</td>
		</tr>
		<tr>
			<th><label for="postalcode"><?php _e("Postal Code"); ?></label></th>
			<td>
				<input type="text" name="postalcode" id="postalcode" value="<?php echo $postalcode; ?>" class="regular-text" /><br />
				<span class="description"><?php _e("Please enter your postal code."); ?></span>
			</td>
		</tr>
		<tr>
			<th><label for="user_profile"><?php _e("Show User Profile"); ?></label></th>
			<td>
				<ul>
					<li>
						<label>
							<input type="radio" value="yes" name="user_profile"<?php checked( $user_profile, 'yes' ) ?> /> Yes
						</label>
					</li>
					<li>
						<label>
							<input type="radio" value="no" name="user_profile"<?php checked( $user_profile, 'no' ) ?> /> No
						</label>
					</li>
				</ul>
			</td>
		</tr>
	</table>
	<?php
}
?>
<?php
add_action( 'personal_options_update', 'cxc_save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'cxc_save_extra_user_profile_fields' );

function cxc_save_extra_user_profile_fields( $user_id ) {
	
	if( ! isset( $_POST[ '_wpnonce' ] ) || ! wp_verify_nonce( $_POST[ '_wpnonce' ], 'update-user_' . $user_id ) ) {
		return;
	}
	
	if( ! current_user_can( 'edit_user', $user_id ) ) {
		return;
	}

	update_user_meta( $user_id, 'city', sanitize_text_field( $_POST[ 'city' ] ) );
	update_user_meta( $user_id, 'address', sanitize_text_field( $_POST[ 'address' ] ) );
	update_user_meta( $user_id, 'postalcode', sanitize_text_field( $_POST[ 'postalcode' ] ) );
	update_user_meta( $user_id, 'user_profile', sanitize_text_field( $_POST[ 'user_profile' ] ) );

}
?>
<?php
add_filter( 'user_contactmethods', 'cxc_modify_user_contact_methods' );

function cxc_modify_user_contact_methods( $methods ) {

	// Add user contact methods
	$methods['skype']   = __( 'Skype'   ); // add custom fields Skype in contacinfo 
	$methods['twitter'] = __( 'Twitter' ); // add custom fields Twitter in contacinfo 
	$methods['linkedin'] = __( 'Linkdin' ); // add custom fields Linkdin in contacinfo 
	$methods['facebook'] = __( 'Facebook' ); // add custom fields Facebook in contacinfo 

	// Remove user contact methods
	unset( $methods['twitter']    ); // Remove custom fields twitter in contacinfo 
	unset( $methods['facebook'] ); // Remove custom fields facebook in contacinfo 

	return $methods;
}
?>

Was this article helpful?
YesNo

Leave a comment

Your email address will not be published.