Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ashleyfae
Created January 15, 2018 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashleyfae/61ba18919e30975766833c0a5d6ac5c5 to your computer and use it in GitHub Desktop.
Save ashleyfae/61ba18919e30975766833c0a5d6ac5c5 to your computer and use it in GitHub Desktop.
RCP: custom multicheck field
<?php
/**
* Example for adding a custom multicheck field to the
* Restrict Content Pro registration form and profile editors.
*/
/**
* Adds a multicheck field to the registration form an dprofile editor.
*/
function rcp_add_multicheck_field() {
$interests = get_user_meta( get_current_user_id(), 'rcp_interests', true );
if ( ! is_array( $interests ) ) {
$interests = array();
}
?>
<p>
<?php _e( 'Select your interests', 'rcp' ); ?>
</p>
<p>
<input name="rcp_interests[]" id="rcp_interests_art" type="checkbox" value="art" <?php checked( in_array( 'art', $interests ) ); ?>/>
<label for="rcp_interests_art"><?php _e( 'Art', 'rcp' ); ?></label> <br>
<input name="rcp_interests[]" id="rcp_interests_books" type="checkbox" value="books" <?php checked( in_array( 'books', $interests ) ); ?>/>
<label for="rcp_interests_books"><?php _e( 'Books', 'rcp' ); ?></label> <br>
<input name="rcp_interests[]" id="rcp_interests_sports" type="checkbox" value="sports" <?php checked( in_array( 'sports', $interests ) ); ?>/>
<label for="rcp_interests_sports"><?php _e( 'Sports', 'rcp' ); ?></label>
<!-- add more fields as desired -->
</p>
<?php
}
add_action( 'rcp_after_password_registration_field', 'rcp_add_multicheck_field' );
add_action( 'rcp_profile_editor_after', 'rcp_add_multicheck_field' );
/**
* Adds the custom multicheck field to the member edit screen.
*/
function ag_rcp_add_multicheck_member_edit_fields( $user_id = 0 ) {
$interests = get_user_meta( $user_id, 'rcp_interests', true );
?>
<tr valign="top">
<th scope="row" valign="top">
<label for="rcp_interests"><?php _e( 'Interests', 'rcp' ); ?></label>
</th>
<td>
<input name="rcp_interests[]" id="rcp_interests_art" type="checkbox" value="art" <?php checked( in_array( 'art', $interests ) ); ?>/>
<span class="description"><?php _e( 'Art', 'rcp' ); ?></span> <br>
<input name="rcp_interests[]" id="rcp_interests_books" type="checkbox" value="books" <?php checked( in_array( 'books', $interests ) ); ?>/>
<span class="description"><?php _e( 'Books', 'rcp' ); ?></span> <br>
<input name="rcp_interests[]" id="rcp_interests_sports" type="checkbox" value="sports" <?php checked( in_array( 'sports', $interests ) ); ?>/>
<span class="description"><?php _e( 'Sports', 'rcp' ); ?></span>
<!-- add more fields as desired -->
</td>
</tr>
<?php
}
add_action( 'rcp_edit_member_after', 'ag_rcp_add_multicheck_member_edit_fields' );
/**
* Shows an error message if none of the multicheck options are selected.
* Remove this code if you want the multicheck to be optional.
*/
function ag_rcp_validate_multicheck_on_register( $posted ) {
if ( is_user_logged_in() ) {
return;
}
if ( empty( $posted['rcp_interests'] ) || ! is_array( $posted['rcp_interests'] ) ) {
rcp_errors()->add( 'invalid_interests', __( 'Please select at least one interest', 'rcp' ), 'register' );
}
}
add_action( 'rcp_form_errors', 'ag_rcp_validate_multicheck_on_register', 10 );
/**
* Stores the information submitted during registration
*
* Stores all the selected interests in an array.
*/
function ag_rcp_save_multicheck_field_on_register( $posted, $user_id ) {
if ( ! empty( $posted['rcp_interests'] ) && is_array( $posted['rcp_interests'] ) ) {
// First sanitize the array.
$interests = array_map( 'sanitize_text_field', $posted['rcp_interests'] );
// Now save.
update_user_meta( $user_id, 'rcp_interests', $interests );
}
}
add_action( 'rcp_form_processing', 'ag_rcp_save_multicheck_field_on_register', 10, 2 );
/**
* Stores the information submitted profile update
*
* Stores all the selected interests in an array.
*/
function ag_rcp_save_multicheck_field_on_profile_save( $user_id ) {
if ( ! empty( $_POST['rcp_interests'] ) && is_array( $_POST['rcp_interests'] ) ) {
// First sanitize the array.
$interests = array_map( 'sanitize_text_field', $_POST['rcp_interests'] );
// Now save.
update_user_meta( $user_id, 'rcp_interests', $interests );
} else {
// Delete the user meta if no checkboxes are selected.
delete_user_meta( $user_id, 'rcp_interests' );
}
}
add_action( 'rcp_user_profile_updated', 'ag_rcp_save_multicheck_field_on_profile_save', 10 );
add_action( 'rcp_edit_member', 'ag_rcp_save_multicheck_field_on_profile_save', 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment