Pronouns Field for User Profiles
Adds an optional "Pronouns" field to user profiles and a [wpide_pronouns] shortcode that shows them, for example next to an author's name.
Opens WPIDE on
Code
$wpide_pronouns_field = function ( $user ) {
printf(
'<h2>About you</h2><table class="form-table" role="presentation"><tr><th scope="row"><label for="wpide-pronouns">Pronouns</label></th><td><input type="text" id="wpide-pronouns" name="wpide_pronouns" value="%s" class="regular-text" maxlength="40" placeholder="e.g. she/her, they/them"></td></tr></table>',
esc_attr( get_user_meta( $user->ID, 'wpide_pronouns', true ) )
);
wp_nonce_field( 'wpide_pronouns_' . $user->ID, 'wpide_pronouns_nonce' );
};
add_action( 'show_user_profile', $wpide_pronouns_field );
add_action( 'edit_user_profile', $wpide_pronouns_field );
$wpide_pronouns_save = function ( $user_id ) {
if ( ! isset( $_POST['wpide_pronouns_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['wpide_pronouns_nonce'] ), 'wpide_pronouns_' . $user_id ) || ! current_user_can( 'edit_user', $user_id ) ) {
return;
}
$value = isset( $_POST['wpide_pronouns'] ) ? substr( sanitize_text_field( wp_unslash( $_POST['wpide_pronouns'] ) ), 0, 40 ) : '';
update_user_meta( $user_id, 'wpide_pronouns', $value );
};
add_action( 'personal_options_update', $wpide_pronouns_save );
add_action( 'edit_user_profile_update', $wpide_pronouns_save );
add_shortcode( 'wpide_pronouns', function ( $atts ) {
$atts = shortcode_atts( array( 'user' => 'current' ), $atts, 'wpide_pronouns' );
$user_id = 'author' === $atts['user'] ? (int) get_post_field( 'post_author', get_the_ID() ) : get_current_user_id();
$value = $user_id ? get_user_meta( $user_id, 'wpide_pronouns', true ) : '';
return $value ? '<span class="wpide-pronouns">(' . esc_html( $value ) . ')</span>' : '';
} );
Notes
Use [wpide_pronouns user="author"] inside posts to show the post author's pronouns; the default shows the logged-in user's.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.