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;
}
}
@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

@JoaquinGA
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);

This was the only thing that worked perfectly for me.

To show custom fields in the general tab of the accounts screen and to show the input and save correctly.

Thank you!

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