-
-
Save ashleyfae/3b3b15b540ee106461328edf7eaaf5f9 to your computer and use it in GitHub Desktop.
RCP: custom textarea 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 textarea field to the | |
* Restrict Content Pro registration form and profile editors. | |
*/ | |
/** | |
* Adds a custom textarea field to the registration form and profile editor. | |
*/ | |
function ag_rcp_add_textarea_field() { | |
$comments = get_user_meta( get_current_user_id(), 'rcp_customer_comments', true ); | |
?> | |
<p> | |
<label for="rcp_customer_comments"><?php _e( 'Comments', 'rcp' ); ?></label> | |
<textarea id="rcp_customer_comments" name="rcp_customer_comments"><?php echo esc_textarea( $comments ); ?></textarea> | |
</p> | |
<?php | |
} | |
add_action( 'rcp_after_password_registration_field', 'ag_rcp_add_textarea_field' ); | |
add_action( 'rcp_profile_editor_after', 'ag_rcp_add_textarea_field' ); | |
/** | |
* Adds the custom textarea field to the member edit screen. | |
*/ | |
function ag_rcp_add_textarea_member_edit_field( $user_id = 0 ) { | |
$comments = get_user_meta( $user_id, 'rcp_customer_comments', true ); | |
?> | |
<tr valign="top"> | |
<th scope="row" valign="top"> | |
<label for="rcp_customer_comments"><?php _e( 'Comments', 'rcp' ); ?></label> | |
</th> | |
<td> | |
<textarea id="rcp_customer_comments" name="rcp_customer_comments" class="large-text" rows="10" cols="30"><?php echo esc_textarea( $comments ); ?></textarea> | |
</td> | |
</tr> | |
<?php | |
} | |
add_action( 'rcp_edit_member_after', 'ag_rcp_add_textarea_member_edit_field' ); | |
/** | |
* Determines if there are problems with the registration data submitted. | |
* Remove this block if you want the textarea to be optional. | |
*/ | |
function ag_rcp_validate_textarea_on_register( $posted ) { | |
if ( is_user_logged_in() ) { | |
return; | |
} | |
if ( empty( $posted['rcp_customer_comments'] ) ) { | |
rcp_errors()->add( 'invalid_customer_comments', __( 'Please enter a comment', 'rcp' ), 'register' ); | |
} | |
} | |
add_action( 'rcp_form_errors', 'ag_rcp_validate_textarea_on_register', 10 ); | |
/** | |
* Stores the information submitted during registration. | |
*/ | |
function ag_rcp_save_textarea_field_on_register( $posted, $user_id ) { | |
if ( ! empty( $posted['rcp_customer_comments'] ) ) { | |
update_user_meta( $user_id, 'rcp_customer_comments', wp_filter_nohtml_kses( $posted['rcp_customer_comments'] ) ); | |
} | |
} | |
add_action( 'rcp_form_processing', 'ag_rcp_save_textarea_field_on_register', 10, 2 ); | |
/** | |
* Stores the information submitted during profile update. | |
*/ | |
function ag_rcp_save_textarea_field_on_profile_save( $user_id ) { | |
if ( ! empty( $_POST['rcp_customer_comments'] ) ) { | |
update_user_meta( $user_id, 'rcp_customer_comments', wp_filter_nohtml_kses( $_POST['rcp_customer_comments'] ) ); | |
} | |
} | |
add_action( 'rcp_user_profile_updated', 'ag_rcp_save_textarea_field_on_profile_save', 10 ); | |
add_action( 'rcp_edit_member', 'ag_rcp_save_textarea_field_on_profile_save', 10 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment