Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Capture default user profile fields at Membership Checkout using Register Helper 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 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.