Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidmutero/273bb00b28143948207ddd7a37636bee to your computer and use it in GitHub Desktop.
Save davidmutero/273bb00b28143948207ddd7a37636bee to your computer and use it in GitHub Desktop.
Collect the default WordPress user fields for Website Field URL (user_url) and show on PMPro Checkout, Profile Edit, and Admin Member Edit
<?php
/**
* Add Website Field to PMPro Checkout, Frontend Profile Edit, and Admin Member Edit Screen.
*
* - Adds a "Website" field to checkout, user profile edit, and admin member edit screen.
* - Saves the website URL correctly to the default WordPress `user_url` field.
*/
/**
* Add Website field to PMPro Checkout Form.
*/
function pmpro_add_website_field_to_checkout() {
$user_url = isset($_POST['user_url']) ? esc_url($_POST['user_url']) : '';
?>
<div class="pmpro_form_field">
<label for="user_url" class="pmpro_form_label"><?php esc_html_e('Website', 'paid-memberships-pro'); ?></label>
<input type="url" id="user_url" name="user_url" value="<?php echo esc_attr($user_url); ?>" class="pmpro_form_input">
</div>
<?php
}
add_action('pmpro_checkout_after_password', 'pmpro_add_website_field_to_checkout');
/**
* Save Website field on Membership Checkout.
*/
function pmpro_save_website_field_at_checkout($user_id) {
if (!empty($_POST['user_url'])) {
update_user_meta($user_id, 'user_url', esc_url($_POST['user_url']));
}
}
add_action('pmpro_after_checkout', 'pmpro_save_website_field_at_checkout');
add_action('user_register', 'pmpro_save_website_field_at_checkout');
/**
* Add Website field to Member Profile Edit
*/
function pmpro_add_website_field_to_member_profile_edit($user) {
$user_url = get_user_meta($user->ID, 'user_url', true);
?>
<h2 class="pmpro_font-large"><?php esc_html_e('Additional Information', 'paid-memberships-pro'); ?></h2>
<div class="pmpro_form_field">
<label for="user_url" class="pmpro_form_label"><?php esc_html_e('Website', 'paid-memberships-pro'); ?></label>
<input type="url" id="user_url" name="user_url" value="<?php echo esc_attr($user_url); ?>" class="pmpro_form_input">
</div>
<?php
}
add_action('show_user_profile', 'pmpro_add_website_field_to_member_profile_edit');
add_action('edit_user_profile', 'pmpro_add_website_field_to_member_profile_edit');
add_action('pmpro_show_user_profile', 'pmpro_add_website_field_to_member_profile_edit');
/**
* Save Website field on Member Profile Edit
*/
function pmpro_save_website_field_from_profile_edit($user_id) {
if (!current_user_can('edit_user', $user_id)) {
return false;
}
if (isset($_POST['user_url'])) {
update_user_meta($user_id, 'user_url', esc_url($_POST['user_url']));
}
}
add_action('personal_options_update', 'pmpro_save_website_field_from_profile_edit');
add_action('edit_user_profile_update', 'pmpro_save_website_field_from_profile_edit');
add_action('pmpro_edit_user_profile_update', 'pmpro_save_website_field_from_profile_edit');
/**
* Add Website Field to PMPro Admin Edit Member Screen under "User Info" Panel.
*/
function pmpro_add_website_field_to_admin_member_edit($user) {
$user_url = get_user_meta($user->ID, 'user_url', true);
?>
<h2 class="pmpro_font-large"><?php esc_html_e('Additional Information', 'paid-memberships-pro'); ?></h2>
<table class="form-table">
<tr>
<th><label for="user_url" class="pmpro_form_label"><?php esc_html_e('Website', 'paid-memberships-pro'); ?></label></th>
<td>
<input type="url" id="user_url" name="user_url" value="<?php echo esc_attr($user_url); ?>" class="pmpro_form_input pmpro_admin_website_input">
</td>
</tr>
</table>
<style>
/* Increase the width of the Website input field in the PMPro Admin Edit Member Screen */
.pmpro_admin_website_input {
min-width: 35em !important;
width: 100% !important;
max-width: 100%;
}
</style>
<?php
}
add_action('pmpro_after_membership_level_profile_fields', 'pmpro_add_website_field_to_admin_member_edit');
/**
* Save Website field in Admin PMPro Member Edit Screen.
*/
function pmpro_save_website_field_from_admin_edit($user_id) {
if (!current_user_can('manage_options')) {
return false;
}
if (isset($_POST['user_url'])) {
update_user_meta($user_id, 'user_url', esc_url($_POST['user_url']));
}
}
add_action('pmpro_after_membership_level_profile_fields_update', 'pmpro_save_website_field_from_admin_edit');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment