"How Did You Hear About Us?" on Registration
Asks new members how they found your site when they register and shows the answer in a column on the Users screen.
Opens WPIDE on
Code
// Value => label.
$wpide_referral_options = array(
'search' => 'Search engine',
'social' => 'Social media',
'friend' => 'A friend or colleague',
'podcast' => 'Podcast or video',
'other' => 'Somewhere else',
);
add_action( 'register_form', function () use ( $wpide_referral_options ) {
echo '<p><label for="wpide_referral">How did you hear about us?</label><select name="wpide_referral" id="wpide_referral" class="input"><option value="">Choose one (optional)</option>';
foreach ( $wpide_referral_options as $value => $label ) {
printf( '<option value="%1$s">%2$s</option>', esc_attr( $value ), esc_html( $label ) );
}
echo '</select></p>';
} );
add_action( 'register_new_user', function ( $user_id ) use ( $wpide_referral_options ) {
$value = isset( $_POST['wpide_referral'] ) ? sanitize_key( $_POST['wpide_referral'] ) : '';
if ( isset( $wpide_referral_options[ $value ] ) ) {
update_user_meta( $user_id, 'wpide_referral', $value );
}
} );
add_filter( 'manage_users_columns', function ( $columns ) {
$columns['wpide_referral'] = 'Found us via';
return $columns;
} );
add_filter( 'manage_users_custom_column', function ( $output, $column, $user_id ) use ( $wpide_referral_options ) {
if ( 'wpide_referral' === $column ) {
$value = get_user_meta( $user_id, 'wpide_referral', true );
$output = isset( $wpide_referral_options[ $value ] ) ? esc_html( $wpide_referral_options[ $value ] ) : '—';
}
return $output;
}, 10, 3 );
Notes
Edit $wpide_referral_options to change the choices; answers are stored in the wpide_referral user meta.
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.