Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save champsupertramp/7e8c5b3407af8d4ed2f96577fb28f37c to your computer and use it in GitHub Desktop.
Save champsupertramp/7e8c5b3407af8d4ed2f96577fb28f37c to your computer and use it in GitHub Desktop.
Ultimate Member - Account form Image upload function
/**
* 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' ) );
}
}
@XTard
Copy link

XTard commented Oct 2, 2022

You never close the add_filter on line 5, otherwise the script works for forms on the UM's Accounts page, but not so much on the UM's User page. When you submit a form with a file on the User page, it returns This 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:

<?php

/**
 * This example shows how to add a new tab into the Profile page of the Ultimate Member. 
 * See the article https://docs.ultimatemember.com/article/69-how-do-i-add-my-extra-tabs-to-user-profiles
 *
 * This example adds the tab 'mycustomtab' that contains the field 'description'. You can add your own tabs and fields.
 * Important! Each profile tab has an unique key. Replace 'mycustomtab' to your unique key.
 *
 * You can add this code to the end of the file functions.php in the active theme (child theme) directory.
 * 
 * Ultimate Member documentation: https://docs.ultimatemember.com/
 * Ultimate Member support (for customers): https://ultimatemember.com/support/ticket/
 */
 
/**
 * Add a new Profile tab
 *
 * @param array $tabs
 * @return array
 */
function um_mycustomtab_add_tab( $tabs ) {

	$tabs[ 'mycustomtab' ] = array(
		'name'   => 'My Custom',
		'icon'   => 'um-faicon-pencil',
		'custom' => true
	);

	UM()->options()->options[ 'profile_tab_' . 'mycustomtab' ] = true;

	return $tabs;
}
add_filter( 'um_profile_tabs', 'um_mycustomtab_add_tab', 1000 );

/**
 * Render tab content
 *
 * @param array $args
 */
function um_profile_content_mycustomtab_default( $args ) {
	/* START. You can paste your content here, it's just an example. */
	
	$action = 'mycustomtab';
	
	/* List user fields you want to see in this form. */
	$fields_metakey = array(
		'description'
	);

	$nonce = filter_input( INPUT_POST, '_wpnonce' );
	if( $nonce && wp_verify_nonce( $nonce, $action ) && um_is_myprofile() ) {
		foreach( $fields_metakey as $metakey ) {
			update_user_meta( um_profile_id(), $metakey, filter_input( INPUT_POST, $metakey ) );
		}
		UM()->user()->remove_cache( um_profile_id() );
	}

	$fields = UM()->builtin()->get_specific_fields( implode( ',', $fields_metakey ) );
	?>

	<div class="um">
		<div class="um-form">
			<form method="post">

				<?php
                                      render_form(/*form id*/); // Rendering the form
				?>
                                     // Submitting the form
				<?php if( um_is_myprofile() ) : ?>
					<div class="um-col-alt">
						<div class="um-left">
							<?php wp_nonce_field( $action ); ?>
							<input type="submit" value="<?php esc_attr_e( 'Update', 'ultimate-member' ); ?>" class="um-button" />
						</div>
					</div>
				<?php endif; ?>

			</form>
		</div>
	</div>

	<?php
	/* END. You can paste your content here, it's just an example. */
}
add_action( 'um_profile_content_mycustomtab_default', 'um_profile_content_mycustomtab_default' );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment