Last active
October 2, 2022 15:13
-
-
Save champsupertramp/7e8c5b3407af8d4ed2f96577fb28f37c to your computer and use it in GitHub Desktop.
Ultimate Member - Account form Image upload function
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
/** | |
* Tutorial: https://www.champ.ninja/2020/05/add-a-custom-account-tab-with-profile-form-fields/ | |
*/ | |
add_filter("um_user_pre_updating_files_array", function($files ){ | |
$user_id = get_current_user_id(); | |
if( empty( $user_id ) ) return $files; | |
$new_files = array(); | |
$old_files = array(); | |
$user_basedir = UM()->uploader()->get_upload_user_base_dir( $user_id, true ); | |
foreach ( $files as $key => $filename ) { | |
//move temporary file from temp directory to the correct user directory | |
$temp_file_path = UM()->uploader()->get_core_temp_dir() . DIRECTORY_SEPARATOR . $filename; | |
if ( file_exists( $temp_file_path ) ) { | |
$extra_hash = hash( 'crc32b', current_time('timestamp') ); | |
if ( strpos( $filename , 'stream_photo_' ) !== false ) { | |
$new_filename = str_replace("stream_photo_","stream_photo_{$extra_hash}_", $filename ); | |
} else { | |
$new_filename = str_replace("file_","file_{$extra_hash}_", $filename ); | |
} | |
$submitted = get_user_meta( $user_id, 'submitted', true ); | |
$submitted = ! empty( $submitted ) ? $submitted : array(); | |
$submitted[ $key ] = $new_filename; | |
update_user_meta( $user_id, 'submitted', $submitted ); | |
if ( $move_only ) { | |
$file = $user_basedir . DIRECTORY_SEPARATOR . $filename; | |
if ( rename( $temp_file_path, $file ) ) { | |
$new_files[ $key ] = $filename; | |
} | |
} else { | |
$file = $user_basedir . DIRECTORY_SEPARATOR . $new_filename; | |
if ( rename( $temp_file_path, $file ) ) { | |
$new_files[ $key ] = $new_filename; | |
$old_files[ $key ] = get_user_meta( $user_id, $key, true ); | |
update_user_meta( $user_id, $key, $new_filename ); | |
$file_info = get_transient( "um_{$filename}" ); | |
if ( ! $file_info ) { | |
$file_info = get_user_meta( $user_id, "{$key}_metadata_temp", true ); | |
delete_user_meta( $user_id, "{$key}_metadata_temp" ); | |
} | |
if ( $file_info ) { | |
update_user_meta( $user_id, "{$key}_metadata", $file_info ); | |
delete_transient( "um_{$filename}" ); | |
} | |
} | |
} | |
} | |
} | |
add_action( 'wp_ajax_um_remove_file', 'um_042821_ajax_remove_file' ,1 ); | |
add_action( 'wp_ajax_nopriv_um_remove_file', 'um_042821_ajax_remove_file', 1 ); | |
function um_042821_ajax_remove_file(){ | |
UM()->check_ajax_nonce(); | |
if ( empty( $_POST['src'] ) ) { | |
wp_send_json_error( __( 'Wrong path', 'ultimate-member' ) ); | |
} | |
if ( empty( $_POST['mode'] ) ) { | |
wp_send_json_error( __( 'Wrong mode', 'ultimate-member' ) ); | |
} | |
$src = $_POST['src']; | |
if ( strstr( $src, '?' ) ) { | |
$splitted = explode( '?', $src ); | |
$src = $splitted[0]; | |
} | |
$mode = sanitize_key( $_POST['mode'] ); | |
if ( $mode == 'profile'){ | |
$user_id = absint( $_POST['user_id'] ); | |
if ( ! UM()->roles()->um_current_user_can( 'edit', $user_id ) ) { | |
wp_send_json_error( __( 'You have no permission to edit this user', 'ultimate-member' ) ); | |
} | |
$is_temp = um_is_temp_upload( $src ); | |
if ( ! $is_temp ) { | |
if ( ! empty( $_POST['filename'] ) && file_exists( UM()->uploader()->get_upload_user_base_dir( $user_id ) . DIRECTORY_SEPARATOR . $_POST['filename'] ) ) { | |
wp_send_json_success(); | |
} | |
} | |
} | |
$files = new um\core\Files(); | |
$raw_file = explode("/",$src); | |
$uid = $raw_file[6]; | |
$field_key = $raw_file[5]; | |
$file = get_user_meta( $uid, $field_key, true ); | |
$file_path = UM()->uploader()->get_upload_base_dir() . $uid . DIRECTORY_SEPARATOR . $file; | |
if( file_exists( $file_path ) ){ | |
unlink( $file_path ); | |
delete_user_meta( $uid, $field_key ); | |
delete_user_meta( $uid, $field_key."_metadata" ); | |
delete_transient( "um_{$file}" ); | |
wp_send_json_success(); | |
} | |
if ($files->delete_file( $src ) ) { | |
wp_send_json_success(); | |
} else { | |
wp_send_json_error( __( 'You have no permission to delete this file. ', 'ultimate-member' ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You never close the
add_filter
on line 5, otherwise the script works for forms on the UM'sAccounts page
, but not so much on the UM'sUser page
. When you submit a form with a file on theUser page
, it returnsThis file has been removed.
, but the changes are still saved and the file is stored properly. Weird glitch.Anyways, thanks for the snippet! Saved a lot of time.
EDIT:
On the
User page
I use a fragment of the script from your tabs tutorial (render_form
to be exact) to render a form inside a custom tab made with help from https://docs.ultimatemember.com/article/69-how-do-i-add-my-extra-tabs-to-user-profiles. The exact script I'm using for the tabs is as follows: