Skip to content

Instantly share code, notes, and snippets.

@ashleyfae

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

Last active Dec 15, 2017
Embed
What would you like to do?
RCP: custom radio field
<?php
/**
* Example for adding custom radio fields to the
* Restrict Content Pro registration form and profile editors.
*/
/**
* Adds a custom radio button fields to the registration form and profile editor.
*/
function ag_rcp_add_radio_fields() {
$referrer = get_user_meta( get_current_user_id(), 'rcp_referrer', true );
?>
<p>
<?php _e( 'How did you find out about our membership program?', 'rcp' ); ?>
</p>
<p>
<label for="rcp_referrer_friend">
<input name="rcp_referrer" id="rcp_referrer_friend" type="radio" value="friend" <?php checked( $referrer, 'friend' ); ?>/>
<?php _e( 'From a friend', 'rcp' ); ?>
</label>
<br/>
<label for="rcp_referrer_search">
<input name="rcp_referrer" id="rcp_referrer_search" type="radio" value="search" <?php checked( $referrer, 'search' ); ?>/>
<?php _e( 'Search engine', 'rcp' ); ?>
</label>
<br/>
<label for="rcp_referrer_social">
<input name="rcp_referrer" id="rcp_referrer_social" type="radio" value="social" <?php checked( $referrer, 'social' ); ?>/>
<?php _e( 'Social media', 'rcp' ); ?>
</label>
<br/>
<label for="rcp_referrer_other">
<input name="rcp_referrer" id="rcp_referrer_other" type="radio" value="other" <?php checked( $referrer, 'other' ); ?>/>
<?php _e( 'Other', 'rcp' ); ?>
</label>
</p>
<?php
}
add_action( 'rcp_after_password_registration_field', 'ag_rcp_add_radio_fields' );
add_action( 'rcp_profile_editor_after', 'ag_rcp_add_radio_fields' );
/**
* Adds the custom radio button fields to the member edit screen.
*/
function ag_rcp_add_radio_member_edit_fields( $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>
<label for="rcp_referrer_friend">
<input name="rcp_referrer" id="rcp_referrer_friend" type="radio" value="friend" <?php checked( $referrer, 'friend' ); ?>/>
<?php _e( 'From a friend', 'rcp' ); ?>
</label>
<br/>
<label for="rcp_referrer_search">
<input name="rcp_referrer" id="rcp_referrer_search" type="radio" value="search" <?php checked( $referrer, 'search' ); ?>/>
<?php _e( 'Search engine', 'rcp' ); ?>
</label>
<br/>
<label for="rcp_referrer_social">
<input name="rcp_referrer" id="rcp_referrer_social" type="radio" value="social" <?php checked( $referrer, 'social' ); ?>/>
<?php _e( 'Social media', 'rcp' ); ?>
</label>
<br/>
<label for="rcp_referrer_other">
<input name="rcp_referrer" id="rcp_referrer_other" type="radio" value="other" <?php checked( $referrer, 'other' ); ?>/>
<?php _e( 'Other', 'rcp' ); ?>
</label>
</td>
</tr>
<?php
}
add_action( 'rcp_edit_member_after', 'ag_rcp_add_radio_member_edit_fields' );
/**
* Determines if there are problems with the registration data submitted.
* Remove this code if you want the radio button to be optional.
*/
function ag_rcp_validate_radio_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_radio_on_register', 10 );
/**
* Stores the information submitted during registration.
*/
function ag_rcp_save_radio_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_radio_field_on_register', 10, 2 );
/**
* Stores the information submitted during profile update.
*/
function ag_rcp_save_radio_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_radio_field_on_profile_save', 10 );
add_action( 'rcp_edit_member', 'ag_rcp_save_radio_field_on_profile_save', 10 );
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.