Social Media Fields

Home » Snippets » Social Media 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_register_user_social_media_fields( WP_User $user ) : void {
	$html = '';
	$html .= '<h3>' . __( 'Social Media Profile Links', 'wpturbo' ) . '</h3>';
	$html .= '<table class="form-table">';
		$html .= '<tr>';
			$html .= '<th>';
				$html .= '<label for="user-field-id">' . __( 'Instagram', 'wpturbo' ) . '</label>';
			$html .= '</th>';
			$html .= '<td>';
				$html .= '<input placeholder="" type="url" name="wpturbo-Instagram" id="wpturbo-Instagram" value="' . esc_attr( get_user_meta( $user->ID, 'wpturbo-Instagram', 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_register_user_social_media_fields' );
add_action( 'edit_user_profile', 'wpturbo_register_user_social_media_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_social_media_fields( int $user_id ): bool {
	if ( !current_user_can( 'edit_user', $user_id ) ) {
		return false;
	}
	update_user_meta( $user_id, 'wpturbo-Instagram', $_POST['wpturbo-Instagram'] );
}
add_action( 'personal_options_update', 'wpturbo_save_user_social_media_fields' );
add_action( 'edit_user_profile_update', 'wpturbo_save_user_social_media_fields' );

			

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