Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save champsupertramp/c1f6d83406e9e0425e9e98aaa36fed7d to your computer and use it in GitHub Desktop.
Save champsupertramp/c1f6d83406e9e0425e9e98aaa36fed7d to your computer and use it in GitHub Desktop.
Ultimate Member - adding custom fields in account page and tab
<?php
/* Add fields to account page */
add_action('um_after_account_general', 'showExtraFields', 100);
function showExtraFields()
{
$custom_fields = [
"alternate_email" => "Permanent E-mail Address",
"major" => "Major",
"minor" => "Minor",
"gpa" => "GPA",
"graduation_year" => "Graduation Year",
"graduation_season" => "Graduation Season",
"gpa" => "GPA",
"phone_number" => "Phone Number (XXX-XXX-XXXX)",
"address_1" => "Permanent Address 1",
"address_2" => "Permanent Address 2",
"city" => "City",
"state" => "State",
"zip_code" => "Zip Code"
];
foreach ($custom_fields as $key => $value) {
$fields[ $key ] = array(
'title' => $value,
'metakey' => $key,
'type' => 'select',
'label' => $value,
);
apply_filters('um_account_secure_fields', $fields, 'general' );
$field_value = get_user_meta(um_user('ID'), $key, true) ? : '';
$html = '<div class="um-field um-field-'.$key.'" data-key="'.$key.'">
<div class="um-field-label">
<label for="'.$key.'">'.$value.'</label>
<div class="um-clear"></div>
</div>
<div class="um-field-area">
<input class="um-form-field valid "
type="text" name="'.$key.'"
id="'.$key.'" value="'.$field_value.'"
placeholder=""
data-validate="" data-key="'.$key.'">
</div>
</div>';
echo $html;
}
}
@Selah1337
Copy link

Selah1337 commented Sep 2, 2021

to view&update custom fields in the [ultimatemember_account] page, this is some distilled code from here, to be added to your theme's functions.php:

add_action('um_after_account_general', 'showUMExtraFields', 100);

function showUMExtraFields() {
  $id = um_user('ID');
  $output = '';
  $names = array('phone_number', 'company');

  $fields = array(); 
  foreach( $names as $name )
    $fields[ $name ] = UM()->builtin()->get_specific_field( $name );
  $fields = apply_filters('um_account_secure_fields', $fields, $id);
  foreach( $fields as $key => $data )
    $output .= UM()->fields()->edit_field( $key, $data );

  echo $output;
}

add_action('um_account_pre_update_profile', 'getUMFormData', 100);

function getUMFormData(){
  $id = um_user('ID');
  $names = array('phone_number', 'company');

  foreach( $names as $name )
    update_user_meta( $id, $name, $_POST[$name] );
}

tested with 2.0.13

How would one use this code to display the custom fields after another specific field (such as after the Username field in the Account tab), as well as make the custom fields uneditable? (Similar to the Username field in the account tab)

@theCJMan
Copy link

Hey all
Here is the scenario
Person XYZ register and marks a radio button field I added "usr_isArtist" as Yes and saves his profile
Person ABC browses the profiles and comes across Person XYZ's profile and see he is an artist. Person ABC would like to click on a button on the Profile page of XYZ that will open a WPForms page that contains form to do some inquiry.

I currently added a button on the "Page" where my profile page is displaying the Profile (at the very bottom as I did not know how to add the button as part of the profile "Template / Form")
But coming across this page I think it might be possible to add a button as part of the Profile View page/template/form that states "Inquire about Me" and when the user clicks on this button then the "url" will be containing a query string to be used in the WP Forms page...
The Query string will contain the Artists Name and Surname...

the URL I am playing with is https://bookartist.co.za/booking-inquiry?artist_name={first_name}+{last_name}

so you can view my page: https://bookartist.co.za/user/peterhoven/ with the button at the very bottom, (Not working correctly) and it will take you to the WP Forms page...

Please can you guys assist me.

  1. I would like the button to be added at the top, possibly JUST next to the "First Name" field
  2. It must pass the Artist's First and Last name to the WP Forms page...

I thank you in advance

@hasinasayan
Copy link

hasinasayan commented Oct 19, 2021

Hi everybody !
anyone know how to hide custom fields registration in mail notification admin when its empty? I used the hook "um_profile_submitted__filter" but it don't work , all custom fields always show.
this is my code:
add_filter("um_profile_submitted__filter","um_profile_submitted", 999, 1 );
function um_profile_submitted( $submitted ){
$fields_mail = array();
foreach (unserialize($submitted) as $key => $item){
if (!empty($item)){
$fields_mail[$key] = $item;
}
}
$submitted = serialize($fields_mail);
return $submitted;
}
thanks in advance

@ATICOSEGUNDA
Copy link

Hi there folks! Ultimate code for custom fields, and succesful saving Front-End / Back-End.

Change parameters "FIELD_01" & "FIELD_02" for yours (you can add asmany as you wish).

Paste it in the end of the functions.php file in your theme.

`/**

  • EXTRA ULTIMATE MEMBER
    */

add_action('um_after_account_general', 'showExtraFields', 100);
function showExtraFields()
{
//these are the meta fields created in registration field
$custom_fields = [
"FIELD_01" => "Field 01",
"FIELD_02" => "Field 02",
];
foreach ($custom_fields as $key => $value) {
$fields[ $key ] = array(
'title' => $value,
'metakey' => $key,
'type' => 'select',
'label' => $value,
);
apply_filters('um_account_secure_fields', $fields, 'wall_privacy' );

$field_value = get_user_meta(um_user('ID'), $key, true) ? : '';

$html = '<div class="um-field um-field-'.$key.'" data-key="'.$key.'">
<div class="um-field-label">
<label for="'.$key.'">'.$value.'</label>
<div class="um-clear"></div>
</div>
<div class="um-field-area">
<input class="um-form-field valid "
type="text" name="'.$key.'"
id="'.$key.'" value="'.$field_value.'"
placeholder=""
data-validate="" data-key="'.$key.'">
</div>
</div>';

echo $html;

}

}
//action to update values of custom field
add_action( 'um_account_pre_update_profile', 'my_account_pre_update_profile', 10, 2 );
function my_account_pre_update_profile( $changes, $user_id ) {
update_user_meta( $user_id, 'FIELD_01', $_POST['FIELD_01'] );
update_user_meta( $user_id, 'FIELD_02', $_POST['FIELD_02'] );
}

/**

  • CODE FOR VIEWING "Extra profile information" AREA IN YOUR USERS BACK-END, MATCH IT WITH YOUR CUSTOM NAMES
    */

function mysite_custom_define() {
$custom_meta_fields = array();
$custom_meta_fields['FIELD_01'] = 'Field 01';
$custom_meta_fields['FIELD_02'] = 'Field 02';
return $custom_meta_fields;
}
function mysite_columns($defaults) {
$meta_number = 0;
$custom_meta_fields = mysite_custom_define();
foreach ($custom_meta_fields as $meta_field_name => $meta_disp_name) {
$meta_number++;
$defaults[('mysite-usercolumn-' . $meta_number . '')] = __($meta_disp_name, 'user-column');
}
return $defaults;
}
function mysite_custom_columns($value, $column_name, $id) {
$meta_number = 0;
$custom_meta_fields = mysite_custom_define();
foreach ($custom_meta_fields as $meta_field_name => $meta_disp_name) {
$meta_number++;
if( $column_name == ('mysite-usercolumn-' . $meta_number . '') ) {
return get_the_author_meta($meta_field_name, $id );
}
}
}
function mysite_show_extra_profile_fields($user) {
print('

Extra profile information

');
print('');
$meta_number = 0;
$custom_meta_fields = mysite_custom_define();
foreach ($custom_meta_fields as $meta_field_name => $meta_disp_name) {
$meta_number++;
print('');
print('');
print('');
print('');
}
print('
' . $meta_disp_name . '');
print('
');
print('');
print('
');
}
function mysite_save_extra_profile_fields($user_id) {
if (!current_user_can('edit_user', $user_id))
return false;
$meta_number = 0;
$custom_meta_fields = mysite_custom_define();
foreach ($custom_meta_fields as $meta_field_name => $meta_disp_name) {
$meta_number++;
update_usermeta( $user_id, $meta_field_name, $_POST[$meta_field_name] );
}
}
add_action('show_user_profile', 'mysite_show_extra_profile_fields');
add_action('edit_user_profile', 'mysite_show_extra_profile_fields');
add_action('personal_options_update', 'mysite_save_extra_profile_fields');
add_action('edit_user_profile_update', 'mysite_save_extra_profile_fields');
add_action('manage_users_custom_column', 'mysite_custom_columns', 15, 3);
add_filter('manage_users_columns', 'mysite_columns', 15, 1);`

That's all, I hope it helps some of you 🌊

@ivanoconfa
Copy link

Thank you so much for this @ATICOSEGUNDA , it is the only code that worked on the most recent UM version. Do you know if we can also show the custom fields value in another page? I need to show UM custom fields of a custom post author (city, address, etc) in the posts they created for everyone to see.

@ATICOSEGUNDA
Copy link

@ivanoconfa you can and should do that without UM, in my case I use Elementor with queries and is so easy to show post author infos. Good luck!

@ivanoconfa
Copy link

Thank you @ATICOSEGUNDA , I will check Elementor now

@hasinasayan
Copy link

Hi guys ! can i ask here how to allow upload mutliple file in field upload ultimate member please??
thanks in advance.

@AzmayenFayek
Copy link

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.
I added this code for uploading profile image during registration. Everything is working fine.

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;

}

@ayoubkhan58
Copy link

/**

EXTRA ULTIMATE MEMBER
*/
add_action('um_after_account_general', 'showExtraFields', 100);
function showExtraFields()
{
//these are the meta fields created in registration field
$custom_fields = [
		"c_user_contry" => "Country",
		"c_complete_address" => "Complete Address",
		"c_cnic" => " CNIC (If Pakistani)",
		"f_profile_fiverr" => "Fiverr Link",
		"f_profile_upwork" => "Upwork Link",
		"f_profile_guru" => "Guru Link",
		"f_profile_freelancer" => "Freelancer Link",
		"f_profile_pph" => "PeoplePerHour Link",
		"sp_fb" => "Facebook",
		"sp_tw" => "Twitter",
		"sp_li" => "LinkedIn",
		"sp_yt" => "YouTube",
		"sp_gh" => "Github",
		"sp_ig" => "Instagram",
];
foreach ($custom_fields as $key => $value) {
$fields[ $key ] = array(
'title' => $value,
'metakey' => $key,
'type' => 'select',
'label' => $value,
);
apply_filters('um_account_secure_fields', $fields, 'wall_privacy' );

$field_value = get_user_meta(um_user('ID'), $key, true) ? : '';

$html = '<div class="um-field um-field-'.$key.'" data-key="'.$key.'">
<div class="um-field-label">
<label for="'.$key.'">'.$value.'</label>
<div class="um-clear"></div>
</div>
<div class="um-field-area">
<input class="um-form-field valid "
type="text" name="'.$key.'"
id="'.$key.'" value="'.$field_value.'"
placeholder=""
data-validate="" data-key="'.$key.'">
</div>
</div>';

echo $html;
}

}

//action to update values of custom field
add_action( 'um_account_pre_update_profile', 'my_account_pre_update_profile', 10, 2 );
function my_account_pre_update_profile( $changes, $user_id ) {
update_user_meta( $user_id, 'c_user_contry', $_POST['c_user_contry'] );
update_user_meta( $user_id, 'c_complete_address', $_POST['c_complete_address'] );
update_user_meta( $user_id, 'c_cnic', $_POST['c_cnic'] );
update_user_meta( $user_id, 'f_profile_fiverr', $_POST['f_profile_fiverr'] );
update_user_meta( $user_id, 'f_profile_upwork', $_POST['f_profile_upwork'] );
update_user_meta( $user_id, 'f_profile_guru', $_POST['f_profile_guru'] );
update_user_meta( $user_id, 'f_profile_freelancer', $_POST['f_profile_freelancer'] );
update_user_meta( $user_id, 'f_profile_pph', $_POST['f_profile_pph'] );
update_user_meta( $user_id, 'sp_fb', $_POST['sp_fb'] );
update_user_meta( $user_id, 'sp_tw', $_POST['sp_tw'] );
update_user_meta( $user_id, 'sp_li', $_POST['sp_li'] );
update_user_meta( $user_id, 'sp_yt', $_POST['sp_yt'] );
update_user_meta( $user_id, 'sp_gh', $_POST['sp_gh'] );
update_user_meta( $user_id, 'sp_ig', $_POST['sp_ig'] );
}

/**

CODE FOR VIEWING "Extra profile information" AREA IN YOUR USERS BACK-END, MATCH IT WITH YOUR CUSTOM NAMES
*/
function mysite_custom_define() {
$custom_meta_fields = array();
$custom_meta_fields['c_user_contry'] = 'Country';
$custom_meta_fields['c_complete_address'] = 'Complete Address';
$custom_meta_fields['c_cnic'] = 'CNIC (If Pakistani)';
$custom_meta_fields['f_profile_fiverr'] = 'Fiverr Link';
$custom_meta_fields['f_profile_upwork'] = 'Upwork Link';
$custom_meta_fields['f_profile_guru'] = 'Guru Link';
$custom_meta_fields['f_profile_freelancer'] = 'Freelancer Link';
$custom_meta_fields['f_profile_pph'] = 'PeoplePerHour Link';
$custom_meta_fields['sp_fb'] = 'Facebook';
$custom_meta_fields['sp_tw'] = 'Twitter';
$custom_meta_fields['sp_li'] = 'LinkedIn';
$custom_meta_fields['sp_yt'] = 'YouTube';
$custom_meta_fields['sp_gh'] = 'Github';
$custom_meta_fields['sp_ig'] = 'Instagram';
return $custom_meta_fields;
}
function mysite_columns($defaults) {
  $meta_number = 0;
  $custom_meta_fields = mysite_custom_define();
  foreach ($custom_meta_fields as $meta_field_name => $meta_disp_name) {
    $meta_number++;
    $defaults[('mysite-usercolumn-' . $meta_number . '')] = __($meta_disp_name, 'user-column');
  }
  return $defaults;
}
function mysite_custom_columns($value, $column_name, $id) {
  $meta_number = 0;
  $custom_meta_fields = mysite_custom_define();
  foreach ($custom_meta_fields as $meta_field_name => $meta_disp_name) {
    $meta_number++;
    if( $column_name == ('mysite-usercolumn-' . $meta_number . '') ) {
      return get_the_author_meta($meta_field_name, $id );
    }
  }
}
function mysite_show_extra_profile_fields($user) {
print('

Extra profile information
');
print('');
$meta_number = 0;
$custom_meta_fields = mysite_custom_define();
foreach ($custom_meta_fields as $meta_field_name => $meta_disp_name) {
  $meta_number++;
  print('');
  print('');
  print('');
  print('');
}
print('
' . $meta_disp_name . '	');
print('
');
print('');
print('
');
}
function mysite_save_extra_profile_fields($user_id) {
  if (!current_user_can('edit_user', $user_id))
  return false;
  $meta_number = 0;
  $custom_meta_fields = mysite_custom_define();

  foreach ($custom_meta_fields as $meta_field_name => $meta_disp_name) {
    $meta_number++;
    update_usermeta( $user_id, $meta_field_name, $_POST[$meta_field_name] );
  }
}
add_action('show_user_profile', 'mysite_show_extra_profile_fields');
add_action('edit_user_profile', 'mysite_show_extra_profile_fields');
add_action('personal_options_update', 'mysite_save_extra_profile_fields');
add_action('edit_user_profile_update', 'mysite_save_extra_profile_fields');
add_action('manage_users_custom_column', 'mysite_custom_columns', 15, 3);
add_filter('manage_users_columns', 'mysite_columns', 15, 1);

@ManikantaAlla
Copy link

Is anyone can help me to place a validation in myaccount while updating customized fields

@ashique12009
Copy link

to view&update custom fields in the [ultimatemember_account] page, this is some distilled code from here, to be added to your theme's functions.php:

add_action('um_after_account_general', 'showUMExtraFields', 100);

function showUMExtraFields() {
  $id = um_user('ID');
  $output = '';
  $names = array('phone_number', 'company');

  $fields = array(); 
  foreach( $names as $name )
    $fields[ $name ] = UM()->builtin()->get_specific_field( $name );
  $fields = apply_filters('um_account_secure_fields', $fields, $id);
  foreach( $fields as $key => $data )
    $output .= UM()->fields()->edit_field( $key, $data );

  echo $output;
}

add_action('um_account_pre_update_profile', 'getUMFormData', 100);

function getUMFormData(){
  $id = um_user('ID');
  $names = array('phone_number', 'company');

  foreach( $names as $name )
    update_user_meta( $id, $name, $_POST[$name] );
}

tested with 2.0.13

How would one use this code to display the custom fields after another specific field (such as after the Username field in the Account tab), as well as make the custom fields uneditable? (Similar to the Username field in the account tab)

This one works good, thanks!

@MisTerM7
Copy link

`<?php
/* Add fields to account page */
add_action('um_after_account_general', 'showExtraFields', 100);
function showExtraFields()
{
$custom_fields = [
"alternate_email" => "Permanent E-mail Address",
"major" => "Major",
"minor" => "Minor",
"gpa" => "GPA",
"graduation_year" => "Graduation Year",
"graduation_season" => "Graduation Season",
"gpa" => "GPA",
"phone_number" => "Phone Number (XXX-XXX-XXXX)",
"address_1" => "Permanent Address 1",
"address_2" => "Permanent Address 2",
"city" => "City",
"state" => "State",
"zip_code" => "Zip Code"
];

foreach ($custom_fields as $key => $value) {

	$fields[ $key ] = array(
			'title' => $value,
			'metakey' => $key,
			'type' => 'select',
			'label' => $value,
	);

	apply_filters('um_account_secure_fields', $fields, 'general' );

	$field_value = get_user_meta(um_user('ID'), $key, true) ? : '';

	$html = '<div class="um-field um-field-'.$key.'" data-key="'.$key.'">
	<div class="um-field-label">
	<label for="'.$key.'">'.$value.'</label>
	<div class="um-clear"></div>
	</div>
	<div class="um-field-area">
	<input class="um-form-field valid "
	type="text" name="'.$key.'"
	id="'.$key.'" value="'.$field_value.'"
	placeholder=""
	data-validate="" data-key="'.$key.'">
	</div>
	</div>';

	echo $html;

}

}`

Hello
I don't know if this is the right tutorial for displaying custom fields.
My problem is that when I want to display the predefined field "user_mail", it displays nothing. I don't have an email address, as you know, you need one to register, but it works.
How do I display it?
Mz

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