Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rifkyfu32/7ec5b89358469f921fb6b0902ff945ea to your computer and use it in GitHub Desktop.
Save rifkyfu32/7ec5b89358469f921fb6b0902ff945ea to your computer and use it in GitHub Desktop.
Customization allow everyone to upload profile on registration pages - Ultimate Member 2.0 +
1.Copy the bellow code in your theme's function.php file. Should be in public_html/wp-content/themes/your-themes-name/function.php
/**
* Ultimate Member 2.0 - Customization
* Description: Allow everyone to upload profile registration pages.
*/
add_filter("um_user_pre_updating_files_array","um_custom_user_pre_updating_files_array", 10, 1);
function um_custom_user_pre_updating_files_array( $arr_files ){
if( is_array( $arr_files ) ){
foreach( $arr_files as $key => $details ){
if( $key == "user_photo" ){
unset( $arr_files[ $key ] );
$arr_files[ "profile_photo" ] = $details;
}
}
}
return $arr_files;
}
add_filter("um_allow_frontend_image_uploads","um_custom_allow_frontend_image_uploads",10, 3);
function um_custom_allow_frontend_image_uploads( $allowed, $user_id, $key ){
if( $key == "profile_photo" ){
return true;
}
return $allowed; // false
}
/* End of customization in your theme's function.php file*/
2. In ultimate member add 'upload image' field in registration form. Enter meta key as 'user_photo' (i.e same as in the code above).
3. Edit file um-short-functions.php. Should be in /public_html/wp-content/plugins/ultimate-member/includes/um-short-functions.php. In line 1760 function um_get_avatar_uri( $image, $attrs ) change with code below :
/**
* Ultimate Member 2.0 - Customization
* Description: Change path image with $custom_profile_photo data from get_user_meta.
*/
function um_get_avatar_uri( $image, $attrs ) {
$uri = false;
$find = false;
$ext = '.' . pathinfo( $image, PATHINFO_EXTENSION );
$custom_profile_photo = get_user_meta(um_user( 'ID' ), 'profile_photo', 'true');
$cache_time = apply_filters( 'um_filter_avatar_cache_time', current_time( 'timestamp' ), um_user( 'ID' ) );
if( $attrs == 'original' && file_exists( um_user_uploads_dir() . "profile_photo{$ext}" ) ) {
$uri = um_user_uploads_uri() . "profile_photo{$ext}";
} else if ( file_exists( um_user_uploads_dir() . $custom_profile_photo ) ) {
$uri = um_user_uploads_uri() . $custom_profile_photo;
} else if ( file_exists( um_user_uploads_dir() . "profile_photo-{$attrs}x{$attrs}{$ext}" ) ) {
$uri = um_user_uploads_uri() . "profile_photo-{$attrs}x{$attrs}{$ext}";
} else if ( file_exists( um_user_uploads_dir() . "profile_photo-{$attrs}{$ext}" ) ) {
$uri = um_user_uploads_uri() . "profile_photo-{$attrs}{$ext}";
} else {
$sizes = UM()->options()->get( 'photo_thumb_sizes' );
if ( is_array( $sizes ) ) $find = um_closest_num( $sizes, $attrs );
if ( file_exists( um_user_uploads_dir() . "profile_photo-{$find}x{$find}{$ext}" ) ) {
$uri = um_user_uploads_uri() . "profile_photo-{$find}x{$find}{$ext}";
}else if ( file_exists( um_user_uploads_dir() . "profile_photo-{$find}{$ext}" ) ) {
$uri = um_user_uploads_uri() . "profile_photo-{$find}{$ext}";
} else if ( file_exists( um_user_uploads_dir() . "profile_photo{$ext}" ) ) {
$uri = um_user_uploads_uri() . "profile_photo{$ext}";
}
}
if ( !empty( $cache_time ) ) {
$uri .= "?{$cache_time}";
}
return $uri;
}
/* End of customization in um-short-functions.php file*/
4. Enjoy it!
@nishani7234
Copy link

For new version of UM:

function um_get_avatar_uri( $image, $attrs ) {
	$uri = false;
	$uri_common = false;
	$find = false;
	$ext = '.' . pathinfo( $image, PATHINFO_EXTENSION );
	$custom_profile_photo = get_user_meta(um_user( 'ID' ), 'profile_photo', 'true');

	if ( is_multisite() ) {
		//multisite fix for old customers
		$multisite_fix_dir = UM()->uploader()->get_upload_base_dir();
		$multisite_fix_url = UM()->uploader()->get_upload_base_url();
		$multisite_fix_dir = str_replace( DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . get_current_blog_id() . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $multisite_fix_dir );
		$multisite_fix_url = str_replace( '/sites/' . get_current_blog_id() . '/', '/', $multisite_fix_url );


		if ( $attrs == 'original' && file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo{$ext}" ) ) {
			$uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo{$ext}";		
		} elseif ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$attrs}x{$attrs}{$ext}" ) ) {
			$uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo-{$attrs}x{$attrs}{$ext}";
		} elseif ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$attrs}{$ext}" ) ) {
			$uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo-{$attrs}{$ext}";
		} else {
			$sizes = UM()->options()->get( 'photo_thumb_sizes' );
			if ( is_array( $sizes ) ) {
				$find = um_closest_num( $sizes, $attrs );
			}

			if ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$find}x{$find}{$ext}" ) ) {
				$uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo-{$find}x{$find}{$ext}";
			} elseif ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$find}{$ext}" ) ) {
				$uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo-{$find}{$ext}";
			} elseif ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo{$ext}" ) ) {
				$uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo{$ext}";
			}
		}
	}

	if ( $attrs == 'original' && file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo{$ext}" ) ) {
		$uri = UM()->uploader()->get_upload_base_dir()  . um_user( 'ID' ) . "/profile_photo{$ext}";
	} elseif ( file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . $custom_profile_photo ) ) {
		$uri = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . $custom_profile_photo;		
	} elseif ( file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$attrs}x{$attrs}{$ext}" ) ) {
		$uri = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . "/profile_photo-{$attrs}x{$attrs}{$ext}";
	} elseif ( file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$attrs}{$ext}" ) ) {
		$uri = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . "/profile_photo-{$attrs}{$ext}";
	} else {
		$sizes = UM()->options()->get( 'photo_thumb_sizes' );
		if ( is_array( $sizes ) ) {
			$find = um_closest_num( $sizes, $attrs );
		}

		if ( file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$find}x{$find}{$ext}" ) ) {
			$uri = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . "/profile_photo-{$find}x{$find}{$ext}";
		} elseif ( file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$find}{$ext}" ) ) {
			$uri = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . "/profile_photo-{$find}{$ext}";
		} elseif ( file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo{$ext}" ) ) {
			$uri = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . "/profile_photo{$ext}";
		}
	}

	if ( ! empty( $uri_common ) && empty( $uri ) ) {
		$uri = $uri_common;
	}

	$cache_time = apply_filters( 'um_filter_avatar_cache_time', current_time( 'timestamp' ), um_user( 'ID' ) );
	if ( ! empty( $cache_time ) ) {
		$uri .= "?{$cache_time}";
	}

	return $uri;
}

I don't know PHP.
I paste this code on 'code snippet' plugin, it shows the error "Cannot redeclare function um_get_avatar_uri"

"The snippet has been deactivated due to an error on line 2:
Cannot redeclare function um_get_avatar_uri"

What's the problem???

@asokr
Copy link

asokr commented Dec 24, 2020

For new version of UM:

function um_get_avatar_uri( $image, $attrs ) {
	$uri = false;
	$uri_common = false;
	$find = false;
	$ext = '.' . pathinfo( $image, PATHINFO_EXTENSION );
	$custom_profile_photo = get_user_meta(um_user( 'ID' ), 'profile_photo', 'true');

	if ( is_multisite() ) {
		//multisite fix for old customers
		$multisite_fix_dir = UM()->uploader()->get_upload_base_dir();
		$multisite_fix_url = UM()->uploader()->get_upload_base_url();
		$multisite_fix_dir = str_replace( DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . get_current_blog_id() . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $multisite_fix_dir );
		$multisite_fix_url = str_replace( '/sites/' . get_current_blog_id() . '/', '/', $multisite_fix_url );


		if ( $attrs == 'original' && file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo{$ext}" ) ) {
			$uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo{$ext}";		
		} elseif ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$attrs}x{$attrs}{$ext}" ) ) {
			$uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo-{$attrs}x{$attrs}{$ext}";
		} elseif ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$attrs}{$ext}" ) ) {
			$uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo-{$attrs}{$ext}";
		} else {
			$sizes = UM()->options()->get( 'photo_thumb_sizes' );
			if ( is_array( $sizes ) ) {
				$find = um_closest_num( $sizes, $attrs );
			}

			if ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$find}x{$find}{$ext}" ) ) {
				$uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo-{$find}x{$find}{$ext}";
			} elseif ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$find}{$ext}" ) ) {
				$uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo-{$find}{$ext}";
			} elseif ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo{$ext}" ) ) {
				$uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo{$ext}";
			}
		}
	}

	if ( $attrs == 'original' && file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo{$ext}" ) ) {
		$uri = UM()->uploader()->get_upload_base_dir()  . um_user( 'ID' ) . "/profile_photo{$ext}";
	} elseif ( file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . $custom_profile_photo ) ) {
		$uri = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . $custom_profile_photo;		
	} elseif ( file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$attrs}x{$attrs}{$ext}" ) ) {
		$uri = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . "/profile_photo-{$attrs}x{$attrs}{$ext}";
	} elseif ( file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$attrs}{$ext}" ) ) {
		$uri = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . "/profile_photo-{$attrs}{$ext}";
	} else {
		$sizes = UM()->options()->get( 'photo_thumb_sizes' );
		if ( is_array( $sizes ) ) {
			$find = um_closest_num( $sizes, $attrs );
		}

		if ( file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$find}x{$find}{$ext}" ) ) {
			$uri = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . "/profile_photo-{$find}x{$find}{$ext}";
		} elseif ( file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$find}{$ext}" ) ) {
			$uri = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . "/profile_photo-{$find}{$ext}";
		} elseif ( file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo{$ext}" ) ) {
			$uri = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . "/profile_photo{$ext}";
		}
	}

	if ( ! empty( $uri_common ) && empty( $uri ) ) {
		$uri = $uri_common;
	}

	$cache_time = apply_filters( 'um_filter_avatar_cache_time', current_time( 'timestamp' ), um_user( 'ID' ) );
	if ( ! empty( $cache_time ) ) {
		$uri .= "?{$cache_time}";
	}

	return $uri;
}

I don't know PHP.
I paste this code on 'code snippet' plugin, it shows the error "Cannot redeclare function um_get_avatar_uri"

"The snippet has been deactivated due to an error on line 2:
Cannot redeclare function um_get_avatar_uri"

What's the problem???

You must replace existing function, do not add code - replace...

@AzmayenFayek
Copy link

AzmayenFayek commented Nov 15, 2021

How can I change the directory of profile photos from "wp-content/uploads/ultimatemember" file to wordpress media library during uploading. So that I can use those image later on directly taking from media.

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