Skip to content

Instantly share code, notes, and snippets.

@pippinsplugins
Last active December 15, 2021 17:07
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pippinsplugins/11402562 to your computer and use it in GitHub Desktop.
Save pippinsplugins/11402562 to your computer and use it in GitHub Desktop.
Custom user fields for Restrict Content Pro
<?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 );
@cinghaman
Copy link

When I export the csv it does not show these fields? do we need to add something additional filter for that

@iamandrewpeters
Copy link

Is there a way to do this...but for group account fields? Like adding fields beyond name/description?

@whdsolutions
Copy link

whdsolutions commented Nov 27, 2020

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