Skip to content

Instantly share code, notes, and snippets.

@ashleyfae

ashleyfae/rcp-custom-field-select.php Secret

Last active Sep 28, 2020
Embed
What would you like to do?
RCP: custom select field
<?php
/**
* Example for adding a custom select field to the
* Restrict Content Pro registration form and profile editors.
*/
/**
* Adds a custom select field to the registration form and profile editor.
*/
function ag_rcp_add_select_field() {
$referrer = get_user_meta( get_current_user_id(), 'rcp_referrer', true );
?>
<p>
<label for="rcp_referrer"><?php _e( 'How did you find out about our membership program?', 'rcp' ); ?></label>
<select id="rcp_referrer" name="rcp_referrer">
<option value="friend" <?php selected( $referrer, 'friend'); ?>><?php _e( 'From a friend', 'rcp' ); ?></option>
<option value="search" <?php selected( $referrer, 'search'); ?>><?php _e( 'Search engine', 'rcp' ); ?></option>
<option value="social" <?php selected( $referrer, 'social'); ?>><?php _e( 'Social media', 'rcp' ); ?></option>
<option value="other" <?php selected( $referrer, 'other'); ?>><?php _e( 'Other', 'rcp' ); ?></option>
</select>
</p>
<?php
}
add_action( 'rcp_after_password_registration_field', 'ag_rcp_add_select_field' );
add_action( 'rcp_profile_editor_after', 'ag_rcp_add_select_field' );
/**
* Adds the custom select field to the member edit screen.
*/
function ag_rcp_add_select_member_edit_field( $user_id = 0 ) {
$referrer = get_user_meta( $user_id, 'rcp_referrer', true );
?>
<tr valign="top">
<th scope="row" valign="top">
<label for="rcp_referrer"><?php _e( 'Referred By', 'rcp' ); ?></label>
</th>
<td>
<select id="rcp_referrer" name="rcp_referrer">
<option value="friend" <?php selected( $referrer, 'friend'); ?>><?php _e( 'From a friend', 'rcp' ); ?></option>
<option value="search" <?php selected( $referrer, 'search'); ?>><?php _e( 'Search engine', 'rcp' ); ?></option>
<option value="social" <?php selected( $referrer, 'social'); ?>><?php _e( 'Social media', 'rcp' ); ?></option>
<option value="other" <?php selected( $referrer, 'other'); ?>><?php _e( 'Other', 'rcp' ); ?></option>
</select>
</td>
</tr>
<?php
}
add_action( 'rcp_edit_member_after', 'ag_rcp_add_select_member_edit_field' );
/**
* Determines if there are problems with the registration data submitted.
*/
function ag_rcp_validate_select_on_register( $posted ) {
if ( is_user_logged_in() ) {
return;
}
// List all the available options that can be selected.
$available_choices = array(
'friend',
'search',
'social',
'other'
);
// Add an error message if the submitted option isn't one of our valid choices.
if ( ! in_array( $posted['rcp_referrer'], $available_choices ) ) {
rcp_errors()->add( 'invalid_referrer', __( 'Please select a valid referrer', 'rcp' ), 'register' );
}
}
add_action( 'rcp_form_errors', 'ag_rcp_validate_select_on_register', 10 );
/**
* Stores the information submitted during registration.
*/
function ag_rcp_save_select_field_on_register( $posted, $user_id ) {
if ( ! empty( $posted['rcp_referrer'] ) ) {
update_user_meta( $user_id, 'rcp_referrer', sanitize_text_field( $posted['rcp_referrer'] ) );
}
}
add_action( 'rcp_form_processing', 'ag_rcp_save_select_field_on_register', 10, 2 );
/**
* Stores the information submitted during profile update.
*/
function ag_rcp_save_select_field_on_profile_save( $user_id ) {
// List all the available options that can be selected.
$available_choices = array(
'friend',
'search',
'social',
'other'
);
if ( isset( $_POST['rcp_referrer'] ) && in_array( $_POST['rcp_referrer'], $available_choices ) ) {
update_user_meta( $user_id, 'rcp_referrer', sanitize_text_field( $_POST['rcp_referrer'] ) );
}
}
add_action( 'rcp_user_profile_updated', 'ag_rcp_save_select_field_on_profile_save', 10 );
add_action( 'rcp_edit_member', 'ag_rcp_save_select_field_on_profile_save', 10 );
@cinghaman

This comment has been minimized.

Copy link

@cinghaman cinghaman commented Sep 28, 2020

Hi, is there way to use file field I am looking for a way to upload user profile image on registration or member edit screen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.