Skip to content

Instantly share code, notes, and snippets.

@ashleyfae

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

Last active Dec 15, 2017
Embed
What would you like to do?
RCP: custom email field
<?php
/**
* Example for adding a custom email field to the
* Restrict Content Pro registration form and profile editors.
*/
/**
* Adds a custom email field to the registration form and profile editor.
*/
function ag_rcp_add_email_field() {
$alternate_email = get_user_meta( get_current_user_id(), 'rcp_alt_email', true );
?>
<p>
<label for="rcp_alt_email"><?php _e( 'Your Alternate Email Address', 'rcp' ); ?></label>
<input type="email" id="rcp_alt_email" name="rcp_alt_email" value="<?php echo esc_attr( $alternate_email ); ?>"/>
</p>
<?php
}
add_action( 'rcp_after_password_registration_field', 'ag_rcp_add_email_field' );
add_action( 'rcp_profile_editor_after', 'ag_rcp_add_email_field' );
/**
* Adds the custom email field to the member edit screen.
*/
function ag_rcp_add_email_member_edit_field( $user_id = 0 ) {
$alternate_email = get_user_meta( $user_id, 'rcp_alt_email', true );
?>
<tr valign="top">
<th scope="row" valign="top">
<label for="rcp_alt_email"><?php _e( 'Alternate Email', 'rcp' ); ?></label>
</th>
<td>
<input type="email" id="rcp_alt_email" name="rcp_alt_email" value="<?php echo esc_attr( $alternate_email ); ?>"/>
</td>
</tr>
<?php
}
add_action( 'rcp_edit_member_after', 'ag_rcp_add_email_member_edit_field' );
/**
* Determines if there are problems with the registration data submitted.
*/
function ag_rcp_validate_email_on_register( $posted ) {
if ( is_user_logged_in() ) {
return;
}
// Remove this segment if you don't want the email to be required.
if ( empty( $posted['rcp_alt_email'] ) ) {
rcp_errors()->add( 'missing_alt_email', __( 'Please enter an alternate email address', 'rcp' ), 'register' );
return;
}
// This throws an error if an email is entered but it's not a valid email address.
if ( ! empty( $posted['rcp_alt_email'] ) && ! is_email( $posted['rcp_alt_email'] ) ) {
rcp_errors()->add( 'invalid_alt_email', __( 'Please enter a valid alternate email address', 'rcp' ), 'register' );
}
}
add_action( 'rcp_form_errors', 'ag_rcp_validate_email_on_register', 10 );
/**
* Stores the information submitted during registration.
*/
function ag_rcp_save_email_field_on_register( $posted, $user_id ) {
if ( ! empty( $posted['rcp_alt_email'] ) ) {
update_user_meta( $user_id, 'rcp_alt_email', sanitize_email( $posted['rcp_alt_email'] ) );
}
}
add_action( 'rcp_form_processing', 'ag_rcp_save_email_field_on_register', 10, 2 );
/**
* Stores the information submitted during profile update.
*/
function ag_rcp_save_email_field_on_profile_save( $user_id ) {
if ( ! empty( $_POST['rcp_alt_email'] ) && is_email( $_POST['rcp_alt_email'] ) ) {
update_user_meta( $user_id, 'rcp_alt_email', sanitize_email( $_POST['rcp_alt_email'] ) );
}
}
add_action( 'rcp_user_profile_updated', 'ag_rcp_save_email_field_on_profile_save', 10 );
add_action( 'rcp_edit_member', 'ag_rcp_save_email_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.