-
-
Save ashleyfae/3f84a3b2b7d0aecc998e53bf970a26bb to your computer and use it in GitHub Desktop.
RCP: custom number field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Example for adding a custom number field to the | |
* Restrict Content Pro registration form and profile editors. | |
*/ | |
/** | |
* Adds a custom number field to the registration form and profile editor. | |
*/ | |
function ag_rcp_add_number_field() { | |
$age = get_user_meta( get_current_user_id(), 'rcp_age', true ); | |
?> | |
<p> | |
<label for="rcp_age"><?php _e( 'Your Age', 'rcp' ); ?></label> | |
<input type="number" id="rcp_age" name="rcp_age" value="<?php echo esc_attr( $age ); ?>"/> | |
</p> | |
<?php | |
} | |
add_action( 'rcp_after_password_registration_field', 'ag_rcp_add_number_field' ); | |
add_action( 'rcp_profile_editor_after', 'ag_rcp_add_number_field' ); | |
/** | |
* Adds the custom number field to the member edit screen. | |
*/ | |
function ag_rcp_add_number_member_edit_field( $user_id = 0 ) { | |
$age = get_user_meta( $user_id, 'rcp_age', true ); | |
?> | |
<tr valign="top"> | |
<th scope="row" valign="top"> | |
<label for="rcp_age"><?php _e( 'Age', 'rcp' ); ?></label> | |
</th> | |
<td> | |
<input type="number" id="rcp_age" name="rcp_age" value="<?php echo esc_attr( $age ); ?>"/> | |
</td> | |
</tr> | |
<?php | |
} | |
add_action( 'rcp_edit_member_after', 'ag_rcp_add_number_member_edit_field' ); | |
/** | |
* Determines if there are problems with the registration data submitted. | |
*/ | |
function ag_rcp_validate_number_on_register( $posted ) { | |
if ( is_user_logged_in() ) { | |
return; | |
} | |
// Remove this segment if you don't want the number field to be required. | |
if ( empty( $posted['rcp_age'] ) ) { | |
rcp_errors()->add( 'missing_age', __( 'Please enter your age', 'rcp' ), 'register' ); | |
return; | |
} | |
// This throws an error if the value is not an number. | |
if ( ! is_numeric( $posted['rcp_age'] ) ) { | |
rcp_errors()->add( 'invalid_age', __( 'Please enter a valid age', 'rcp' ), 'register' ); | |
} | |
} | |
add_action( 'rcp_form_errors', 'ag_rcp_validate_number_on_register', 10 ); | |
/** | |
* Stores the information submitted during registration. | |
* We're using `absint()` to make sure the number is a positive integer. If you want | |
* to allow for negative numbers too, you can use `intval()` instead. | |
*/ | |
function ag_rcp_save_number_field_on_register( $posted, $user_id ) { | |
if ( ! empty( $posted['rcp_age'] ) ) { | |
update_user_meta( $user_id, 'rcp_age', absint( $posted['rcp_age'] ) ); | |
} | |
} | |
add_action( 'rcp_form_processing', 'ag_rcp_save_number_field_on_register', 10, 2 ); | |
/** | |
* Stores the information submitted during profile update. | |
* We're using `absint()` to make sure the number is a positive integer. If you want | |
* to allow for negative numbers too, you can use `intval()` instead. | |
*/ | |
function ag_rcp_save_number_field_on_profile_save( $user_id ) { | |
if ( ! empty( $_POST['rcp_age'] ) ) { | |
update_user_meta( $user_id, 'rcp_age', absint( $_POST['rcp_age'] ) ); | |
} | |
} | |
add_action( 'rcp_user_profile_updated', 'ag_rcp_save_number_field_on_profile_save', 10 ); | |
add_action( 'rcp_edit_member', 'ag_rcp_save_number_field_on_profile_save', 10 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment