Public Contact Details User Fields

Home » Snippets » Public Contact Details User Fields
0

Created with:

User Fields Generator

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
				/**
 * Adds custom fields to the user profile edit page.
 *
 * @param WP_User $user The user object being edited.
 * 
 * @return void
 */
function wpturbo_user_contact_details_fields( WP_User $user ) : void {
	$html = '';
	$html .= '<h3>' . __( 'Contact Details', 'wpturbo' ) . '</h3>';
	$html .= '<table class="form-table">';
		$html .= '<tr>';
			$html .= '<th>';
				$html .= '<label for="user-field-id">' . __( 'Public Contact Email', 'wpturbo' ) . '</label>';
			$html .= '</th>';
			$html .= '<td>';
				$html .= '<input placeholder="" type="email" name="wpturbo-public-contact-email" id="wpturbo-public-contact-email" value="' . esc_attr( get_user_meta( $user->ID, 'wpturbo-public-contact-email', true ) ) . '" class="regular-text" /><br />';
				$html .= '<span class="description">' . __( '', 'wpturbo' ) . '</span>';
			$html .= '</td>';
		$html .= '</tr>';
	$html .= '</table>';
	echo $html;
}
add_action( 'show_user_profile', 'wpturbo_user_contact_details_fields' );
add_action( 'edit_user_profile', 'wpturbo_user_contact_details_fields' );

/**
 * Saves custom user fields data when the user profile is updated.
 *
 * @param int $user_id The user ID being updated.
 * 
 * @return bool Whether or not the user data was successfully updated.
 */
function wpturbo_save_user_contact_details_fields( int $user_id ): bool {
	if ( !current_user_can( 'edit_user', $user_id ) ) {
		return false;
	}
	update_user_meta( $user_id, 'wpturbo-public-contact-email', $_POST['wpturbo-public-contact-email'] );
}
add_action( 'personal_options_update', 'wpturbo_save_user_contact_details_fields' );
add_action( 'edit_user_profile_update', 'wpturbo_save_user_contact_details_fields' );

			

Register an account to save your snippets or go Pro to get more features.