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/2f88849447f85175eb161b19d4551119 to your computer and use it in GitHub Desktop.
Save ipokkel/2f88849447f85175eb161b19d4551119 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 and save with custom callback using save_function for RH
<?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 );
}
@ipokkel
Copy link
Author

ipokkel commented Mar 18, 2021

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

For capturing URL and Biographical Info with additional Registration checks see:
https://gist.github.com/ipokkel/431a589fc323794686deaf854bf708da

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