Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ipokkel/431a589fc323794686deaf854bf708da to your computer and use it in GitHub Desktop.
Save ipokkel/431a589fc323794686deaf854bf708da 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.
<?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' );
@ipokkel
Copy link
Author

ipokkel commented Mar 18, 2021

For capturing URL and Biographical Info without an additional registration and validation check see:
https://gist.github.com/ipokkel/2f88849447f85175eb161b19d4551119

For capturing URL only Info see:
https://gist.github.com/ipokkel/6590d29c21e41a49e7e2577fe9ca7955

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment