Forked from ipokkel/example-pmpro-save-function-with-registration-check.php
Created
August 17, 2021 16:23
-
-
Save MaryOJob/3ce0b35dc91d3f7e4f389dc17dfd5f74 to your computer and use it in GitHub Desktop.
Collect the default WordPress Website URL and Biographical description during Membership Checkout with Register Helper fields using save_function custom callback that includes a validation check which returns an error message if the field value failed the check.
This file contains hidden or 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 | |
/** | |
* This recipe adds Website and Biographical Info to the Membership Checkout. | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
function my_default_wp_user_checkout_fields() { | |
if ( class_exists( 'PMProRH_Field' ) ) { | |
// create custom checkout box | |
pmprorh_add_checkout_box( 'additional', 'Additional Information' ); | |
// Conditional to show on your profile and hide on user edit profile | |
$your_profile = false; | |
if ( ! is_admin() ) { | |
$your_profile = true; | |
} | |
// create array. | |
$fields = array(); | |
// user_url field | |
$fields[] = new PMProRH_Field( | |
'url', | |
'text', | |
array( | |
'label' => 'Website url', // Change label here | |
'size' => 40, // Change size here | |
'profile' => $your_profile, // Hide on profile page | |
'required' => false, // Make field optional (set to true to make required) | |
'save_function' => 'my_pmprorh_callback_update_my_user_url', // Custom callback to save field values | |
) | |
); | |
// user_description field | |
$fields[] = new PMProRH_Field( | |
'description', | |
'textarea', | |
array( | |
'label' => 'Biographical Info', // Change label here | |
'profile' => $your_profile, // Hide on profile page | |
'required' => false, // Make field optional (set to true to make required) | |
'save_function' => 'my_pmprorh_callback_update_usermeta_textarea', | |
'sanitize' => false, | |
) | |
); | |
foreach ( $fields as $field ) { | |
pmprorh_add_registration_field( 'additional', $field ); | |
} | |
} | |
} | |
add_action( 'init', 'my_default_wp_user_checkout_fields' ); | |
function my_pmprorh_callback_update_my_user_url( $user_id, $field_name, $field_value ) { | |
// update wp_user table | |
wp_update_user( | |
array( | |
'ID' => $user_id, | |
'user_url' => esc_url_raw( $field_value ), | |
) | |
); | |
// duplicate value in wp_usermeta table so RH can access field value | |
update_user_meta( $user_id, $field_name, esc_url_raw( $field_value ) ); | |
} | |
function my_pmprorh_callback_update_usermeta_textarea( $user_id, $field_name, $field_value ) { | |
// santize text area content | |
$txtarea = wp_kses( $field_value, wp_kses_allowed_html( 'pre_user_description' ) ); | |
update_user_meta( $user_id, $field_name, $txtarea ); | |
} | |
// Check if url starts with protocol and | |
function my_pmpro_check_valid_url( $pmpro_continue_registration ) { | |
// bail if not okay to continue | |
if ( ! $pmpro_continue_registration ) { | |
return $pmpro_continue_registration; | |
} | |
if ( isset( $_REQUEST['url'] ) ) { | |
// get url field value | |
$url = $_REQUEST['url']; | |
// check if it starts with protocol | |
if ( ! empty( $url ) && 1 <= strpos( $url, 'http' ) ) { | |
// things are not okay | |
$pmpro_continue_registration = false; | |
// notify user on checkout page. | |
pmpro_setMessage( 'Please enter a full URL starting with http:// or https:// ' . strpos( $url, 'http' ), 'pmpro_error' ); | |
} | |
} | |
return $pmpro_continue_registration; | |
} | |
add_filter( 'pmpro_registration_checks', 'my_pmpro_check_valid_url' ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment