Last active
November 6, 2024 09:12
-
-
Save ipokkel/7a026f6002232a56e1dc448ff58da6fd to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
/** | |
* Allow users to add an avatar at checkout for the Basic User Avatars plugin. | |
* | |
* This recipe assumes that you have the Basic User Avatar plugin installed and activated. | |
* @link https://www.paidmembershipspro.com/add-ons/basic-user-avatar/ | |
* | |
* This recipe requires a custom user field created with the user field name set to basic_user_avatar. | |
* | |
* 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 user_field_file_upload_to_basic_user_avatars( $user_id ) { | |
// Let's only do this if the Basic User Avatar plugin is active. | |
if ( ! class_exists( 'basic_user_avatars' ) ) { | |
return; | |
} | |
// Get the user. | |
$user = get_user_by( 'id', $user_id ); | |
$basic_user_avatar = false; | |
// Check for avatar in user meta if the user is logged in. | |
if ( is_user_logged_in() ) { | |
// Get the user's avatar array. | |
$basic_user_avatar = get_user_meta( $user_id, 'basic_user_avatar', false ); | |
} | |
// Let's bail if we don't have the required user meta. | |
if ( empty( $basic_user_avatar ) ) { | |
return; | |
} | |
// Get the upload directory and path. | |
$upload_dir = wp_upload_dir(); | |
$upload_path = $upload_dir['basedir'] . '/pmpro-register-helper/' . $user->user_login . '/'; | |
$upload_url = $upload_dir['baseurl'] . '/pmpro-register-helper/' . $user->user_login . '/'; | |
// Let's check if basic_user_avatar meta exists. | |
if ( ! empty( $basic_user_avatar ) ) { | |
$basic_user_avatar = $basic_user_avatar[0]; | |
// Let's check if the basic_user_avatar meta data is in the PMPro file upload format. | |
if ( ! isset( $basic_user_avatar['full'] ) && isset( $basic_user_avatar['fullurl'] ) && ! empty( $basic_user_avatar['fullurl'] ) ) { | |
// Let's create the new basic_user_avatar array. | |
$new_basic_user_avatar_array = array( | |
'full' => $basic_user_avatar['fullurl'], | |
); | |
// Let's create a avatar file path array. | |
$basic_user_avatar_path_array = array( | |
'full' => $basic_user_avatar['fullpath'], | |
); | |
// Get the filename. | |
$filename = $basic_user_avatar['filename']; | |
// Get the file extension. | |
$file_extension = pathinfo( $filename, PATHINFO_EXTENSION ); | |
// Image sizes for the avatar. | |
$size = array( 32, 64, 128, 256 ); | |
// Loop through the image sizes and create the new images. | |
foreach ( $size as $s ) { | |
// create the new image | |
$image = wp_get_image_editor( $upload_path . $filename ); | |
if ( ! is_wp_error( $image ) ) { | |
$image->resize( $s, $s, true ); | |
$image->save( $upload_path . $filename . '-' . $s . 'x' . $s . '.' . $file_extension ); | |
// add the new image to the array. | |
$new_basic_user_avatar_array[ $s ] = $upload_url . $filename . '-' . $s . 'x' . $s . '.' . $file_extension; | |
$basic_user_avatar_path_array[ $s . '_path' ] = $upload_path . $filename . '-' . $s . 'x' . $s . '.' . $file_extension; | |
} | |
} | |
// Let's make sure all the required images exists. | |
$all_images_exist = true; | |
foreach ( $basic_user_avatar_path_array as $path ) { | |
if ( ! file_exists( $path ) ) { | |
$all_images_exist = false; | |
break; | |
} | |
} | |
// Update the user meta if all the images exist. | |
if ( $all_images_exist ) { | |
wp_delete_file( $basic_user_avatar['previewpath'] ); // We don't need the old preview image anymore, lets delete it. | |
update_user_meta( $user_id, 'basic_user_avatar', $new_basic_user_avatar_array ); // Set the meta for Basic User Avatars. | |
} | |
} | |
} | |
} | |
add_action( 'pmpro_after_checkout', 'user_field_file_upload_to_basic_user_avatars', 10, 1 ); | |
/* | |
The display on profile option for this field should be set to "No" for the display on the profile option for either | |
the group (Show fields on user profile?) or the field (Show field on user profile?). | |
Let's ensure that the user field is not displayed on the user profile page. | |
*/ | |
function remove_bua_field_from_profile( $field, $where ) { | |
if ( 'basic_user_avatar' === $field->name ) { | |
if ( true === $field->profile ) { | |
$field->profile = 'only'; | |
} | |
} | |
return $field; | |
} | |
add_filter( 'pmpro_add_user_field', 'remove_bua_field_from_profile', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you would like to allow users to update their avatar you can add the
[basic-user-avatars]
shortcode to a page, e.g. on the Member Profile Edit page below the member edit profile shortcode or block.