Last active
December 15, 2021 17:07
-
-
Save pippinsplugins/11402562 to your computer and use it in GitHub Desktop.
Custom user fields for Restrict Content Pro
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 | |
/* | |
Plugin Name: Restrict Content Pro - Custom User Fields | |
Description: Illustrates how to add custom user fields to the Restrict Content Pro registration form that can also be edited by the site admins | |
Version: 1.0 | |
Author: Pippin Williamson | |
Author URI: http://pippinsplugins.com | |
Contributors: mordauk | |
*/ | |
/** | |
* Adds the custom fields to the registration form and profile editor | |
* | |
*/ | |
function pw_rcp_add_user_fields() { | |
$profession = get_user_meta( get_current_user_id(), 'rcp_profession', true ); | |
$location = get_user_meta( get_current_user_id(), 'rcp_location', true ); | |
?> | |
<p> | |
<label for="rcp_profession"><?php _e( 'Your Profession', 'rcp' ); ?></label> | |
<input name="rcp_profession" id="rcp_profession" type="text" value="<?php echo esc_attr( $profession ); ?>"/> | |
</p> | |
<p> | |
<label for="rcp_location"><?php _e( 'Your Location', 'rcp' ); ?></label> | |
<input name="rcp_location" id="rcp_location" type="text" value="<?php echo esc_attr( $location ); ?>"/> | |
</p> | |
<?php | |
} | |
add_action( 'rcp_before_subscription_form_fields', 'pw_rcp_add_user_fields' ); | |
add_action( 'rcp_profile_editor_after', 'pw_rcp_add_user_fields' ); | |
/** | |
* Adds the custom fields to the member edit screen | |
* | |
*/ | |
function pw_rcp_add_member_edit_fields( $user_id = 0 ) { | |
$profession = get_user_meta( $user_id, 'rcp_profession', true ); | |
$location = get_user_meta( $user_id, 'rcp_location', true ); | |
?> | |
<tr valign="top"> | |
<th scope="row" valign="top"> | |
<label for="rcp_profession"><?php _e( 'Profession', 'rcp' ); ?></label> | |
</th> | |
<td> | |
<input name="rcp_profession" id="rcp_profession" type="text" value="<?php echo esc_attr( $profession ); ?>"/> | |
<p class="description"><?php _e( 'The member\'s profession', 'rcp' ); ?></p> | |
</td> | |
</tr> | |
<tr valign="top"> | |
<th scope="row" valign="top"> | |
<label for="rcp_profession"><?php _e( 'Location', 'rcp' ); ?></label> | |
</th> | |
<td> | |
<input name="rcp_location" id="rcp_location" type="text" value="<?php echo esc_attr( $location ); ?>"/> | |
<p class="description"><?php _e( 'The member\'s location', 'rcp' ); ?></p> | |
</td> | |
</tr> | |
<?php | |
} | |
add_action( 'rcp_edit_member_after', 'pw_rcp_add_member_edit_fields' ); | |
/** | |
* Determines if there are problems with the registration data submitted | |
* | |
*/ | |
function pw_rcp_validate_user_fields_on_register( $posted ) { | |
if( empty( $posted['rcp_profession'] ) ) { | |
rcp_errors()->add( 'invalid_profession', __( 'Please enter your profession', 'rcp' ), 'register' ); | |
} | |
if( empty( $posted['rcp_location'] ) ) { | |
rcp_errors()->add( 'invalid_location', __( 'Please enter your location', 'rcp' ), 'register' ); | |
} | |
} | |
add_action( 'rcp_form_errors', 'pw_rcp_validate_user_fields_on_register', 10 ); | |
/** | |
* Stores the information submitted during registration | |
* | |
*/ | |
function pw_rcp_save_user_fields_on_register( $posted, $user_id ) { | |
if( ! empty( $posted['rcp_profession'] ) ) { | |
update_user_meta( $user_id, 'rcp_profession', sanitize_text_field( $posted['rcp_profession'] ) ); | |
} | |
if( ! empty( $posted['rcp_location'] ) ) { | |
update_user_meta( $user_id, 'rcp_location', sanitize_text_field( $posted['rcp_location'] ) ); | |
} | |
} | |
add_action( 'rcp_form_processing', 'pw_rcp_save_user_fields_on_register', 10, 2 ); | |
/** | |
* Stores the information submitted profile update | |
* | |
*/ | |
function pw_rcp_save_user_fields_on_profile_save( $user_id ) { | |
if( ! empty( $_POST['rcp_profession'] ) ) { | |
update_user_meta( $user_id, 'rcp_profession', sanitize_text_field( $_POST['rcp_profession'] ) ); | |
} | |
if( ! empty( $_POST['rcp_location'] ) ) { | |
update_user_meta( $user_id, 'rcp_location', sanitize_text_field( $_POST['rcp_location'] ) ); | |
} | |
} | |
add_action( 'rcp_user_profile_updated', 'pw_rcp_save_user_fields_on_profile_save', 10 ); | |
add_action( 'rcp_edit_member', 'pw_rcp_save_user_fields_on_profile_save', 10 ); |
I think one step is missing.
after the user fills the form and goes to the payment gateway but clicks the back button it comes to the registration page but if you click the Register button it doesn't pull the saved inform from database and the error message for these custom fields.
When I export the csv it does not show these fields? do we need to add something additional filter for that
Is there a way to do this...but for group account fields? Like adding fields beyond name/description?
Does not work for me :( - Been trying for days!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Pippin
I used your code to add custom fields to my RCP plugin and I am super glad that it worked. nice work.
But can i ask, what needs to be altered so the fields are not mandatory? I want field #2 (location) to be optional. And do you know if they kan be visible and have a separate column on the RCP costumer page?
Best regards
Runolfur.