Last active
November 2, 2020 22:03
-
-
Save MaryOJob/e81fb7a18068c9ef1a090e7328d3d3a0 to your computer and use it in GitHub Desktop.
Added Custom Fields for Website, Profile Picture, & Syncing of Biographical Info field with Character Limit, all via Register Helper Add On (PMPro)
This file contains 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 // Do not copy this tag please. Copy from below. | |
/** | |
* See the PMPro Register Helper readme for more information and examples: https://www.paidmembershipspro.com/documentation/register-helper-documentation/adding-fields/ | |
* Please add the below code to your custom plugin or Code Snippets Plugin by following this guide - 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(); | |
$fields[] = new PMProRH_Field( | |
'user_avatar', // input name, will also be used as meta key | |
'file', // type of field | |
array( | |
'label' => 'Profile Picture', | |
'hint' => 'Recommended size is 100px X 100px', | |
'profile' => true, // show in user profile | |
'addmember' => true, | |
) | |
); | |
// user_description field | |
$fields[] = new PMProRH_Field( | |
'description', | |
'textarea', | |
array( | |
'label' => 'Short Bio', // Change label here | |
'profile' => $your_profile, // Hide on profile page | |
'required' => true, // Make field optional (set to true to make required) | |
'save_function' => 'my_pmprorh_callback_update_usermeta_textarea', | |
'rows' => 2, // number of rows | |
'html_attributes' => array( 'maxlength' => '100'), // input the number of characters you want members to be able to type in | |
'sanitize' => false, | |
) | |
); | |
$fields[] = new PMProRH_Field( | |
'website', // input name, will also be used as meta key | |
'text', // type of field | |
array( | |
'label' => 'Website', // custom field label | |
'profile' => true, // show in user profile | |
'required' => true, // make this field required | |
'addmember' => true, // Allows field shows up if using the Add Member Add-On | |
'memberslistcsv' => 'true', // Adds to CSV export of Members List | |
) | |
); | |
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 ); | |
} | |
/* | |
* Add WP User Avatar from Register Helper field during checkout. | |
*/ | |
add_action( 'added_user_meta', 'my_updated_user_meta', 10, 4 ); | |
add_action( 'updated_user_meta', 'my_updated_user_meta', 10, 4 ); | |
function my_updated_user_meta( $meta_id, $user_id, $meta_key, $meta_value ) { | |
// Change user_avatar to your Register Helper file upload name. | |
if ( 'user_avatar' == $meta_key ) { | |
$filename = $meta_value['fullpath']; | |
$filetype = wp_check_filetype( basename( $filename ), null ); | |
$wp_upload_dir = wp_upload_dir(); | |
$attachment = array( | |
'post_mime_type' => $filetype['type'], | |
'post_title' => preg_replace( '/\.[^.]+$/', '', wp_upload_dir() . $filename ), | |
'post_status' => 'inherit', | |
); | |
$attach_id = wp_insert_attachment( $attachment, $filename ); | |
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. | |
require_once ABSPATH . 'wp-admin/includes/image.php'; | |
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); | |
wp_update_attachment_metadata( $attach_id, $attach_data ); | |
update_user_meta( $user_id, 'wp_user_avatar', $attach_id ); | |
} | |
} | |
add_filter( 'get_avatar', 'my_user_avatar_filter', 20, 5 ); | |
function my_user_avatar_filter( $avatar, $id_or_email, $size, $default, $alt ) { | |
$my_user = get_userdata( $id_or_email ); | |
if ( ! empty( $my_user ) ) { | |
$avatar_id = get_user_meta( $my_user->ID, 'wp_user_avatar', true ); | |
if ( ! empty( $avatar_id ) ) { | |
$avatar = wp_get_attachment_url( $avatar_id, $size ); | |
$avatar = "<img alt='{$alt}' src='{$avatar}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />"; | |
} | |
} | |
return $avatar; | |
} | |
add_action( 'init', 'my_pmprorh_init_avatar_logo' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment