Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
}
@Kearsan

This comment has been minimized.

Copy link

@Kearsan Kearsan commented Mar 20, 2017

Nice code, works perfectly!

Is there a way to check some validation conditions ? For example unique meta value, url etc... Juste like fields in profile ?

Kind regards,

@oziguerra

This comment has been minimized.

Copy link

@oziguerra oziguerra commented Apr 15, 2017

Hi!

Quick question, where does this code go?

Thanks!

EDIT: Figured it out, I added it to the end of the functions.php file on the template, thanks!

@matthies1

This comment has been minimized.

Copy link

@matthies1 matthies1 commented Apr 20, 2017

Hi there.

The code works if I edit the field on the profile page, but it doesn't work if I edit the field on the account site.

What I have to do if the field on the account site was edited and dont update after accepting?

Thanks!

@javierjuan

This comment has been minimized.

Copy link

@javierjuan javierjuan commented Jun 28, 2017

Hi,

Same as matthies1. I have a new field in the "Default Registration" form. In the user "Account" page, this field does not appear by default, however with this code I can show the field with the current value the user introduced at registration time.

However, editing this field in the "Account" page and updating the account does not change anything. I tried to catch it with the "um_account_pre_update_profile" action but I do not achieve it.

Could you give us some help?

Thanks!

@arausch

This comment has been minimized.

Copy link

@arausch arausch commented Jul 2, 2017

I'm new to PHP (and github), but I was able to tweak the code to allow it to save. It uses some of the built in functions to get the forms. I also added a variable for fields that should be shown but disabled. Thank you @champsupertramp, I wouldn't have been able to get here without your code!

add_action('um_after_account_general', 'showExtraFields', 100);
function showExtraFields() {
  global $ultimatemember;

  $names = array('phone_number','zip_code');
  $disabled = array('zip_code');

  $fields = get_custom_fields( $names );
  $fields = apply_filters('um_account_secure_fields', $fields, $id );

  foreach( $fields as $key => $data ){
    if (in_array($key,$disabled)) {
      $fieldOutput = $ultimatemember->fields->edit_field( $key, $data );
      $output .= str_replace('id="' . $key . '"', 'id="' . $key . '"' . 'disabled="disabled"', $fieldOutput);
    } else {
      $output .= $ultimatemember->fields->edit_field( $key, $data );
    }
  }
  echo $output;
}

function get_custom_fields( $fields ) {
  global $ultimatemember;
  $array=array();
  foreach ($fields as $field ) {
    if ( isset( $ultimatemember->builtin->saved_fields[$field] ) ) {
      $array[$field] = $ultimatemember->builtin->saved_fields[$field];
    } else if ( isset( $ultimatemember->builtin->predefined_fields[$field] ) ) {
      $array[$field] = $ultimatemember->builtin->predefined_fields[$field];
    }
  }
  return $array;
}
@Nugerama

This comment has been minimized.

Copy link

@Nugerama Nugerama commented Jul 26, 2017

Thanks @arausch. I had some trouble getting your get_custom_fields() function to work but did some digging and found the built in function which I used directly. My edited code below.

add_action('um_after_account_general', 'show_extra_fields', 100);
function show_extra_fields() {

  global $ultimatemember;
  $id = um_user('ID');
  $output = '';

  $names = [ 'mobile_number', 'facebook', 'linkedin', 'position', 'country' ];

  $fields = [];
  foreach( $names as $name )
    $fields[ $name ] = $ultimatemember->builtin->get_specific_field( $name );

  $fields = apply_filters( 'um_account_secure_fields', $fields, $id );

  foreach( $fields as $key => $data )
    $output .= $ultimatemember->fields->edit_field( $key, $data );

  echo $output;
}
@carlviclim

This comment has been minimized.

Copy link

@carlviclim carlviclim commented Aug 12, 2017

Works well on my account tab.
You have for registration also?

@elitzer2

This comment has been minimized.

Copy link

@elitzer2 elitzer2 commented Aug 13, 2017

Hmm. I still can't seem to update/save fields on the account page with this code.

@dwlorimer

This comment has been minimized.

Copy link

@dwlorimer dwlorimer commented Aug 17, 2017

Update/Save is not working on custom fields for me either. This code used to work, but doesn't not anymore. It's almost like 'update_user_metadata' isn't triggering on an edit/save.

@Ownsmash

This comment has been minimized.

Copy link

@Ownsmash Ownsmash commented Aug 23, 2017

Thanks for sharing! This is allowing me to see custom fields on the Account Page, however, it doesn't seem to be pulling in the data for the fields. They are all blank even though a user registered and filled in the fields on the register form. Any idea why this is happening?

@champsupertramp

This comment has been minimized.

Copy link
Owner Author

@champsupertramp champsupertramp commented Aug 23, 2017

Hi Everyone, did you try excluding the UM pages from any plugin/server cache? What UM versions are you using?

Regards,

@dwlorimer

This comment has been minimized.

Copy link

@dwlorimer dwlorimer commented Aug 23, 2017

@champsupertramp -- I discovered that in a recent version change, Ultimate member removed the Session variable that stores the fields:

In version 1.3.78, um-filters-account.php had this code:
`add_filter('um_account_secure_fields','um_account_secure_fields', 10, 2);
function um_account_secure_fields( $fields, $tab_key ){
//print_R($fields);
//print_R($tab_key);

	$_SESSION['um_account_fields'][ $tab_key ] = $fields; 

	update_user_meta( um_user('ID'), 'um_account_secure_fields', $_SESSION['um_account_fields'] );
	
	return $fields;
}`

That Session variable has been completely removed in recent versions, which appears to be why the above code no longer saves the field values. I haven't found a solution yet, but maybe this will help us get on the right track.

@Ownsmash

This comment has been minimized.

Copy link

@Ownsmash Ownsmash commented Aug 23, 2017

@champsupertramp I'm using UM v1.3.88 with WP v4.8.1.

At this time I'm not really too concerned with updating my custom fields from the Account Page, mainly I am trying to get the data that saved when a User registered to be displayed in their Account Page. All data that is NOT custom fields populates correctly.

As @dwlorimer said it seems to be related to the um_account_secure_fields function.

@dwlorimer

This comment has been minimized.

Copy link

@dwlorimer dwlorimer commented Aug 24, 2017

I think I have my issue figured out. It's a combination of @champsupertramp 's original, plus the work of @arausch and @Nugerama .

Since recent versions of Ultimate Member, if a field is not listed in the Secure Fields array, it will not be saved. I tested this by setting um_account_secure_fields__enabled to false, and no fields saved at all.

Here is my theory -- it appears that they've also changed how fields are added to the secure fields array, but did not note this change either in the changelog, or the code comments.

The change appears to be this: (Please correct me if you discover otherwise)
Instead of:
apply_filters('um_account_secure_fields', $fields, 'general' );
this line should read
global $ultimatemember; $id = um_user('ID');
...
$fields = apply_filters( 'um_account_secure_fields', $fields, $id );

It appears that UM changed the um_account_secure_fields from wanting the name of the tab, to wanting the id of the user (but their code comments still say $tab_key).

I have not specifically tested @champsupertramps specific code above, because my code is modified from that. Try this change in your codebase and see if it works for you. My custom data is now saving correctly after this change.

@Ownsmash

This comment has been minimized.

Copy link

@Ownsmash Ownsmash commented Aug 25, 2017

Okay, so the example from @Nugerama also uses the um_user('ID') and this allows me to update and view custom fields on the Account page. Thanks!

My existing issue is that the custom fields on the Register Form are apparently not being saved...as their value does not show up on the account page custom fields. I have to updated the values at the account page for them to stick...weird.

Thanks for the help so far!

@abdallahmaali

This comment has been minimized.

Copy link

@abdallahmaali abdallahmaali commented Sep 13, 2017

i have same problem , cant edit the user information

@TamaraZambrana

This comment has been minimized.

Copy link

@TamaraZambrana TamaraZambrana commented Sep 25, 2017

Hi,
first thank you very much for the code.

I have de same issue as others persons, I can not save after changing the data. Anyone have an idea?

Thanks!

@elitzer2

This comment has been minimized.

Copy link

@elitzer2 elitzer2 commented Sep 27, 2017

I've been struggling with this for a long while. This does the trick for me too! Thanks everyone!

@MikeKoval

This comment has been minimized.

Copy link

@MikeKoval MikeKoval commented Oct 19, 2017

Hi! Have the same issue, cannot update user information and have empty um_account_secure_fields in db (
I've added extra checking on empty value in um_account_secure_registered_fields and it seems it helped.

if (!empty($secure_fields)) {
            update_user_meta( um_user('ID'), 'um_account_secure_fields', $secure_fields );
}
@mattx2

This comment has been minimized.

Copy link

@mattx2 mattx2 commented Dec 21, 2017

Hey guys,

I tried all different combinations of the code and I can't get any to work on UM 2.0 (required for my setup).
Can someone give me a complete code that works? Thanks!

@pisacode

This comment has been minimized.

Copy link

@pisacode pisacode commented Dec 26, 2017

Hi

This is fully working code for my case, it shows the fields with proper field types and works on also when updating.
Hope it also works for you.

add_action('um_after_account_general', 'show_extra_fields', 100);
function show_extra_fields() {

  global $ultimatemember;
  $id = um_user('ID');
  $output = '';

  $names = [ "phone_number",'job', "company", "tax-number","sector","company-employee-count","company-address"  ];

  $fields = [];
  foreach( $names as $name )
    $fields[ $name ] = $ultimatemember->builtin->get_specific_field( $name );
	global $ultimatemember; $id = um_user('ID');
  $fields = apply_filters( 'um_account_secure_fields', $fields, $id );

  foreach( $fields as $key => $data )
    $output .= $ultimatemember->fields->edit_field( $key, $data );

  echo $output;
}

function get_custom_fields( $fields ) {
  global $ultimatemember;
  $array=array();
  foreach ($fields as $field ) {
    if ( isset( $ultimatemember->builtin->saved_fields[$field] ) ) {
      $array[$field] = $ultimatemember->builtin->saved_fields[$field];
    } else if ( isset( $ultimatemember->builtin->predefined_fields[$field] ) ) {
      $array[$field] = $ultimatemember->builtin->predefined_fields[$field];
    }
  }
  return $array;
}
@mattx2

This comment has been minimized.

Copy link

@mattx2 mattx2 commented Dec 26, 2017

@pisacode

Unfortunately this code doesn't work - it doesn't add any fields and it makes my Update Account button disappear >:
Perhaps it is because I'm using UM 2.0

BTW, I'm adding the code to the bottom of:
/wp-content/plugins/ultimate-member/includes/class-functions.php

@mattx2

This comment has been minimized.

Copy link

@mattx2 mattx2 commented Jan 2, 2018

Anyone do this with UM 2.0 ?

@pisacode

This comment has been minimized.

Copy link

@pisacode pisacode commented Jan 3, 2018

You should add the code to your functions.php file @mattx2

@jnsimex

This comment has been minimized.

Copy link

@jnsimex jnsimex commented Jan 8, 2018

@pisacode

I added your code to my functions.php file and now the phone number field is showing on the accounts page but it doesn't retrieve or update the phone number field. Any reason for this?

@mattx2

This comment has been minimized.

Copy link

@mattx2 mattx2 commented Jan 9, 2018

I added the code to my theme's functions.php file, but I get the same result - no fields are added and the update button is gone.
What UM version are you using?

@Mona-Abdelkader

This comment has been minimized.

Copy link

@Mona-Abdelkader Mona-Abdelkader commented Jan 19, 2018

@pisacode

Thanks a lot it worked for me, but I have another question please, how to change the field label; i.e. change label of phone_number field from Phone Number to Cell phone for example??

Thanks in advance

@shaLVerma

This comment has been minimized.

Copy link

@shaLVerma shaLVerma commented Jan 23, 2018

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

function getFormData(){

get_current_user_id();
update_user_meta( get_current_user_id(), 'school', $_POST['school'] );
update_user_meta( get_current_user_id(), 'phone', $_POST['phone'] );
update_user_meta( get_current_user_id(), 'Interest', $_POST['Interest'] );
update_user_meta( get_current_user_id(), 'qualification_radio', $_POST['qualification_radio'] );
update_user_meta( get_current_user_id(), 'city', $_POST['city'] );
update_user_meta( get_current_user_id(), 'state', $_POST['state'] );
update_user_meta( get_current_user_id(), 'level', $_POST['level'] );

}

@ivanovsky1

This comment has been minimized.

Copy link

@ivanovsky1 ivanovsky1 commented Jan 28, 2018

Hello everyone. I would only like to add an, address, city, state, and zip code, upon registration inside UM default registration form plugin. I actually added it as ("new" text box) which works and shows in registration email. But fails to record the information inside wordpress-users. Can someone help me with this needed code?
Please.

@joanhypeproject

This comment has been minimized.

Copy link

@joanhypeproject joanhypeproject commented Feb 2, 2018

This works for me, I have edited next line:
apply_filters('um_account_secure_fields', $fields, get_current_user_id() );

Code:
`add_action('um_after_account_general', 'showExtraFields', 100);
function showExtraFields()
{
$custom_fields = [
"facebook_profile" => __( 'Facebook profile', 'list' )
];

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

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

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

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

}`

@sausagebeansandchips

This comment has been minimized.

Copy link

@sausagebeansandchips sausagebeansandchips commented Feb 22, 2018

@pisacode Your code is working just about perfectly! Thank you!!

For my benefit and maybe others that are experiencing the same issue, the only problem is that conditional fields are not appearing on the account page, even when the conditions are being met.

Any idea on how to fix this?

@kticka

This comment has been minimized.

Copy link

@kticka kticka commented Mar 12, 2018

It doesn't work. Account fields aren't updated. Meta data with um_account_secure_fields is empty. Support and documentation of this plugin is... awful. Extensions are expensive and buggy. Meh.

@mansurahamed

This comment has been minimized.

Copy link

@mansurahamed mansurahamed commented Mar 20, 2018

For UM version 2.0+ this code works

add_action('um_after_account_general', 'show_extra_fields', 100);
function show_extra_fields() {

  $id = um_user('ID');
  $output = '';

  $names = array( "phone_number","church_name", "company", "tax-number","sector","company-employee-count","company-address");

  $fields = array();
  foreach( $names as $name )
    $fields[ $name ] = UM()->builtin()->get_specific_field( $name );
 $id = um_user('ID');
  $fields = apply_filters( 'um_account_secure_fields', $fields, $id );

  foreach( $fields as $key => $data )
    $output .= UM()->fields()->edit_field( $key, $data );

  echo $output;
}

function get_custom_fields( $fields ) {
  global $ultimatemember;
  $array=array();
  foreach ($fields as $field ) {
    if ( isset( UM()->builtin()->saved_fields[$field] ) ) {
      $array[$field] = UM()->builtin()->saved_fields[$field];
    } else if ( isset( UM()->builtin()->predefined_fields[$field] ) ) {
      $array[$field] = UM()->builtin()->predefined_fields[$field];
    }
  }
  return $array;
}
@ivanchimeno

This comment has been minimized.

Copy link

@ivanchimeno ivanchimeno commented Apr 4, 2018

Thank you @pisacode, I would've never thought of looking inside $ultimatemember->builtin to retrieve defined fields!

@demo7up

This comment has been minimized.

Copy link

@demo7up demo7up commented Apr 11, 2018

Fixes the not saving issue!

/* 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,
	);

	global $ultimatemember; $id = um_user('ID');

	$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;

}

$fields = apply_filters( 'um_account_secure_fields', $fields, $id );
@demo7up

This comment has been minimized.

Copy link

@demo7up demo7up commented Apr 12, 2018

anyone know how I can query individual items from um_cache_userdata

@rkrenn

This comment has been minimized.

Copy link

@rkrenn rkrenn commented May 8, 2018

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

@haydnjames

This comment has been minimized.

Copy link

@haydnjames haydnjames commented May 26, 2018

Has anyone found a way of using this to add the ability to upload files/userphotos this way?

I have a photo upload field on my registration form - which works fine. I would like users to be able to edit that here. I can call the field - but the photos are uploaded to temp files only from the account page. I spoke to UM about this direct, but they said custom fields on the account page were unsupported so wouldn't help.

@pwkip

This comment has been minimized.

Copy link

@pwkip pwkip commented Aug 27, 2018

@rkrenn nice one!

@tsquare27

This comment has been minimized.

Copy link

@tsquare27 tsquare27 commented Aug 28, 2018

@rkrenn you are the best,

Hello, please can someone help me with code that displays all my ultimate member meta data for registered users. Thank you.

@radhex

This comment has been minimized.

Copy link

@radhex radhex commented Sep 14, 2018

Hi, this modification allows You to create REAL custom fields which can then be validated, previous examples checked whether they are only predefined

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

function showExtraFields(){

$custom_fields = [
	"phone" => "Phone",
	"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",
	"pay" => "Pay",
	"zip_code" => "Zip Code"
];

$fields = [];
foreach ($custom_fields as $key => $value) {
	$fields[$key] = array(
			'title' => $value,
			'metakey' => $key,
			'type' => 'text',
			'label' => $value,
			'required' => 1,
			'public' => 1,
			'editable' => 1,
			'validate' => $key
	);
}

$id = um_user('ID');
$fields = apply_filters( 'um_account_secure_fields', $fields, $id );

UM()->builtin()->saved_fields = $fields;
UM()->builtin()->set_custom_fields(); 

foreach( $fields as $key => $data ) 
		$output .= UM()->fields()->edit_field( $key, $data );

echo $output;

}

add_action( 'um_submit_account_errors_hook', 'my_submit_account_errors', 10, 1 );
function my_submit_account_errors( $submitted ) {

global $ultimatemember;
if (strlen($submitted['phone'] ) > 9) 
	$ultimatemember->classes['form']->add_error('phone','Phone is too long');

}

@zishanj

This comment has been minimized.

Copy link

@zishanj zishanj commented Sep 14, 2018

There is already a filter um_account_tab_general_fields but it is broken in latest version which I have fixed in my fork. Here is sample for usage:

add_filter( 'um_account_tab_general_fields', 'showAccountExtraFields', 10, 2 );
function showAccountExtraFields($args, $shortcode_args) {
 $args .= "field1,field2";
 return $args;
}

$args return comma separated fields which you can extend to add your own comma separated custom fields. Rest it will handle itself to display them on the form. You only have to mention the field names. Easy enough! :)

@moonkat

This comment has been minimized.

Copy link

@moonkat moonkat commented Oct 10, 2018

If anyone is still needing this feature, and sifting through the thread to piece together a working code snippet, FYI, the code provided by @mansurahamed works perfectly as-is.

Thanks man! ^^

@alinvel

This comment has been minimized.

Copy link

@alinvel alinvel commented Dec 16, 2018

This is great, but is there any way to show the date and hour on which the account was created or a unique code?
On the user info I have "date_submitted" but I haven't managed to show it on account of each user.
Any help would be greatly appreciated

@robagi

This comment has been minimized.

Copy link

@robagi robagi commented Mar 6, 2019

HI! many people that say they have solved when in fact in my opinion none of the answers or solutions achieve the purpose of the title "adding custom fields".

What is really trying to achieve (and I think that is the purpose of this code) adding a custom field "NEW" instead of reusing a previously created one.

I think many who claim to have been successful with some proposals used fields previously created either pre-established from UM or created by oneself with the illusion of having succeeded. I need someone to clarify.

And I suppose (I hope not mistakenly) that they create them in the visual interface of the "field manager" and then re-use those fields in the code.

I see many examples that talking about fields like: "phone_number", "mobile_number", "job", "company" and I do not see any with an example that says "custom_field_0", "custom_field_1", "custom_field_2" the idea with the initial code is to create NEW fields "I suppose" and not to reuse those already created.

@radhex or @joanhypeproject are very close to the conjecture I am trying to explain, but try your code without any results.

Then I see extracts of code like this that confuse me:

$ names = array ("phone_number", "church_name", "company", "tax-number", "sector", "company-employee-count", "company-address");

Where is the structure of each of these fields of the type:

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

Which is what indicates that we are creating new and not pre-existing fields? or maybe even something like this:

$ fields ['custom_field_0'] = Array ("type" => "select",
"title" => "custom_field_0",
"metakey" => "custom_field_0",
"options" => Array (
"0" => "Custom 1",
"1" => "Custom 2",
"2" => "Custom 3"),
"visibility" => "all",
"label" => "Custom Field",
"placeholder" => "Custom Field",
"public" => 1,
"editable" => 1);

Apparently UM has a some kind of restriction for that purpose because nobody has found the solution to the problem.

With the first code of @champsupertramp in fact the fields are displayed on the screen but they can not be stored.

Has anyone been able to "REALLY STORE NEW CUSTOM FIELDS" that can be seen after clicking the "Update" button?

Thanks!

@Carmichaeldb

This comment has been minimized.

Copy link

@Carmichaeldb Carmichaeldb commented Apr 23, 2019

I have a question, is there a way to have different account pages based on user role? If so how would I do that?

@atmilin

This comment has been minimized.

Copy link

@atmilin atmilin commented May 30, 2019

Hello Guys

How to add state dropdown with predefined values?

Can you please help me out?

@alesvojta

This comment has been minimized.

Copy link

@alesvojta alesvojta commented Jul 29, 2019

This works for me with UM 2.0
Add custom field to account page:

add_action('um_after_account_general', 'after_account_general_custom_fields', 100);
function after_account_general_custom_fields()
{
    $custom_fields = [
        'newsletter_subscription' => [
            'title'    => 'Newsletter subscription',
            'label'    => 'Newsletter subscription',
            'metakey'  => 'newsletter_subscription',
            'type'     => 'checkbox',
            'options' => ['Subscribe'],
            'required' => 0,
            'public'   => 1,
            'editable' => 1,
        ],
    ];

    $fields = apply_filters('um_account_secure_fields', $custom_fields, um_user('ID'));

    UM()->builtin()->saved_fields = $fields;
    UM()->builtin()->set_custom_fields();

    $output = '';
    foreach ($fields as $key => $data) {
        $output .= UM()->fields()->edit_field($key, $data);
    }
    echo $output;
}

Save custom field value:

add_action('um_account_pre_update_profile', 'account_pre_update_profile_newsletter_subscription', 100, 2);
function account_pre_update_profile_newsletter_subscription($changes, $user_id)
{
    $field = 'newsletter_subscription';

    $old_value = get_user_meta($user_id, $field, true);
    $new_value = isset($_POST[$field]) ? $_POST[$field][0] : null;
    if ($old_value != $new_value[0]) {
        $email = um_user('user_email');
        $list_id = DI()->get('mailchimp')['list_id'];
        $api_key = DI()->get('mailchimp')['api_key'];
        $merge_fields = [
            'FNAME' => um_user('first_name'),
            'LNAME' => um_user('last_name'),
        ];
        $status = isset($_POST[$field]) ? 'subscribed' : 'unsubscribed';
        mailchimp_subscriber_status($email, $status, $list_id, $api_key, $merge_fields);
    }

    update_user_meta($user_id, $field, $_POST[$field]);
};
@vrushalipokharkar86

This comment has been minimized.

Copy link

@vrushalipokharkar86 vrushalipokharkar86 commented Jul 31, 2019

Hello all

How to save the custom file upload field and how to show on account page?

Can you please help me out asap?

@ethiel88

This comment has been minimized.

Copy link

@ethiel88 ethiel88 commented Sep 13, 2019

Guys, today is September 13rd, 2019 I have the UM version 2.0.56 and I can say solution from joanhypeproject is ok, but.... that will only work for the first custom field, the other friends who said it worked was because they only had one custom field. I have several custom fields.
So the problem for the other custom fields that are not being saved is the location of the line

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

This line can't be inside the loop foreach, because it triggers the first time for the first custom field, and the others remains untriggered. The solution is put that line after the loop, check my coud ;)

`add_action('um_after_account_general', 'showExtraFields', 100);
function showExtraFields()
{
$custom_fields = [
"razon_social" => "Razón Social",
"nit" => "NIT",
"ci" => "CI",
"phone_number" => "Número de teléfono fijo",
"mobile_number" => "Número de teléfono móvil",
];

foreach ($custom_fields as $key => $value) {
	$fields[ $key ] = array(
			'title' => $value,
			'metakey' => $key,
			'type' => 'select',
			'label' => $value,
	);
	$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;

}
apply_filters('um_account_secure_fields', $fields, get_current_user_id());

}`

Regards from Bolivia!

@ethiel88

This comment has been minimized.

Copy link

@ethiel88 ethiel88 commented Sep 13, 2019

@Sailingmike

This comment has been minimized.

Copy link

@Sailingmike Sailingmike commented Feb 14, 2020

I'm trying to add one additional field to Account page "Membership Number" but can't see how to do it?

@prasaddesai19

This comment has been minimized.

Copy link

@prasaddesai19 prasaddesai19 commented Mar 16, 2020

I am trying to add "Multi Select Dropdown" field on the account page, can any one help me how to add the multi select option field on the account page, the custom field value is not saving after updating the account page. Is there any easy way to add multi select custom field on the account form.
Please someone help me.
Thanks in advance.

@Brucekib

This comment has been minimized.

Copy link

@Brucekib Brucekib commented Apr 3, 2020

Complete code to display custom field on the account page and allow updating

`
add_action('um_after_account_general', 'after_account_general_custom_fields', 100);
function after_account_general_custom_fields()
{
$custom_fields = [
'user_id_number' => [
'title' => 'Custom Field Title',
'label' => 'Custom Field Label',
'metakey' => 'custom_field_name',
'type' => 'text',
'options' => ['Update'],
'required' => 1,
'public' => 1,
'editable' => 1,
],
];

$fields = apply_filters('um_account_secure_fields', $custom_fields, um_user('ID'));

UM()->builtin()->saved_fields = $fields;
UM()->builtin()->set_custom_fields();

$output = '';
foreach ($fields as $key => $data) {
    $output .= UM()->fields()->edit_field($key, $data);
}
echo $output;

}

/*
From Ultimate Member

add_action( 'um_account_pre_update_profile', 'my_account_pre_update_profile', 10, 2 );
function my_account_pre_update_profile( $changes, $user_id ) {
// your code here
}
*/

add_action('um_account_pre_update_profile', 'update_custom_value', 10, 2);

function update_custom_value($changes, $user_id){
// $id = um_user('ID');
$names = array('custom_field_name');

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

`

@zoroastru

This comment has been minimized.

Copy link

@zoroastru zoroastru commented May 3, 2020

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] );
}

Just tried it today with two custom fields and it works like a charm! I'm using version 2.1.5

@ayhanmalkoc

This comment has been minimized.

Copy link

@ayhanmalkoc ayhanmalkoc commented May 9, 2020

Hi, how do I add a "woocommerce bookings" tab to my ultimate member account page. Will Woocommerce bookings "booking" work if php is included directly? I really need this.

@sockod

This comment has been minimized.

Copy link

@sockod sockod commented May 11, 2020

So, is there a working code so we can save to custom fields?

@champsupertramp

This comment has been minimized.

Copy link
Owner Author

@champsupertramp champsupertramp commented May 14, 2020

Hi Everyone,

I'll be posting a new tutorial on my website. I'll share how you can add UM Profile Form to the UM Account form in a separated tab. I'll provide custom codes to add multiple tabs with easy steps. Subscribe to my newsletter on my website: www.champ.ninja

Screen Shot 2020-05-15 at 1 23 10 AM

@alexliptsin

This comment has been minimized.

Copy link

@alexliptsin alexliptsin commented May 26, 2020

Dummy question. Where do you paste it?

@Brucekib

This comment has been minimized.

Copy link

@Brucekib Brucekib commented May 26, 2020

In the functions.php file.

@zoroastru

This comment has been minimized.

Copy link

@zoroastru zoroastru commented May 26, 2020

So, is there a working code so we can save to custom fields?

Yes - the code is in the getUMFormData function.

@alexliptsin

This comment has been minimized.

Copy link

@alexliptsin alexliptsin commented May 27, 2020

In the functions.php file.

Thanks

@alexliptsin

This comment has been minimized.

Copy link

@alexliptsin alexliptsin commented May 27, 2020

HI! many people that say they have solved when in fact in my opinion none of the answers or solutions achieve the purpose of the title "adding custom fields".

What is really trying to achieve (and I think that is the purpose of this code) adding a custom field "NEW" instead of reusing a previously created one.

I think many who claim to have been successful with some proposals used fields previously created either pre-established from UM or created by oneself with the illusion of having succeeded. I need someone to clarify.

And I suppose (I hope not mistakenly) that they create them in the visual interface of the "field manager" and then re-use those fields in the code.

I see many examples that talking about fields like: "phone_number", "mobile_number", "job", "company" and I do not see any with an example that says "custom_field_0", "custom_field_1", "custom_field_2" the idea with the initial code is to create NEW fields "I suppose" and not to reuse those already created.

@radhex or @joanhypeproject are very close to the conjecture I am trying to explain, but try your code without any results.

Then I see extracts of code like this that confuse me:

$ names = array ("phone_number", "church_name", "company", "tax-number", "sector", "company-employee-count", "company-address");

Where is the structure of each of these fields of the type:

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

Which is what indicates that we are creating new and not pre-existing fields? or maybe even something like this:

$ fields ['custom_field_0'] = Array ("type" => "select",
"title" => "custom_field_0",
"metakey" => "custom_field_0",
"options" => Array (
"0" => "Custom 1",
"1" => "Custom 2",
"2" => "Custom 3"),
"visibility" => "all",
"label" => "Custom Field",
"placeholder" => "Custom Field",
"public" => 1,
"editable" => 1);

Apparently UM has a some kind of restriction for that purpose because nobody has found the solution to the problem.

With the first code of @champsupertramp in fact the fields are displayed on the screen but they can not be stored.

Has anyone been able to "REALLY STORE NEW CUSTOM FIELDS" that can be seen after clicking the "Update" button?

Thanks!

Agree. Did you find solution?

@Brucekib

This comment has been minimized.

Copy link

@Brucekib Brucekib commented May 27, 2020

@alexlipstin Maybe this is what you want. Created a new field called - ID number.

`add_action('um_after_account_general', 'after_account_general_custom_fields', 100);
function after_account_general_custom_fields()
{
$custom_fields = [
'user_id_number' => [
'title' => 'ID Number',
'label' => 'ID Number',
'metakey' => 'user_id_number',
'type' => 'text',
'options' => ['Update'],
'required' => 1,
'public' => 1,
'editable' => 1,
],
];

$fields = apply_filters('um_account_secure_fields', $custom_fields, um_user('ID'));

UM()->builtin()->saved_fields = $fields;
UM()->builtin()->set_custom_fields();

$output = '';
foreach ($fields as $key => $data) {
    $output .= UM()->fields()->edit_field($key, $data);
}
echo $output;

}

/*
add_action( 'um_account_pre_update_profile', 'my_account_pre_update_profile', 10, 2 );
function my_account_pre_update_profile( $changes, $user_id ) {
// your code here
}
*/

add_action('um_account_pre_update_profile', 'getUMFormData', 10, 2);

function getUMFormData($changes, $user_id){
// $id = um_user('ID');
$names = array('user_id_number');

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

@champsupertramp

This comment has been minimized.

Copy link
Owner Author

@champsupertramp champsupertramp commented May 27, 2020

Has anyone tried using a Profile Form in the Account form in a custom tab? You should be able to use the Profile Form as a custom form in the Account tab. Please see this tutorial:
https://www.champ.ninja/2020/05/add-a-custom-account-tab-with-profile-form-fields/

@haydnjames

This comment has been minimized.

Copy link

@haydnjames haydnjames commented Jul 10, 2020

@champsupertramp Your solution is almost working for me, but the problem I have is that dropdown fields do not save correctly. It only saves the 'value' which is a number, and not what the actual list option title is.

EDIT
This appears to be the way UM is creating the mark-up for the dropdown. A dropdown on the registration page uses the correct mark-up for options <option value="Marketing">Marketing</option> but when the same field is pulled into another form - a custom profile form in this instance - the mark-up becomes <option value="1">Marketing</option> so the data saved becomes a bit useless. I noticed in your solution you have a 'fix' for radios, does it need one for dropdowns too?

@dtipson

This comment has been minimized.

Copy link

@dtipson dtipson commented Jul 21, 2020

Using $_POST['fieldname'] in um_account_pre_update_profile seems to work for me. Just don't forget to sanitize it!

@negru13

This comment has been minimized.

Copy link

@negru13 negru13 commented Sep 23, 2020

Hey guys! Does anyone know how to add placeholders values to the existing account fields like "First name", "Password" etc.?
I want to style the fields by removing the labels and keeping only the placeholders..

@coffee-sleep

This comment has been minimized.

Copy link

@coffee-sleep coffee-sleep commented Nov 8, 2020

@dtipson Could you tell me what exactly I should do to "sanitize" it? I am new to wordpress/coding and encountering the same issue as @haydnjames.
When I do $_POST['fieldname'] it just clears all data and nothing can be saved (even the pre-saved fields clears up).

I have this code from @rkenn on my functions.php which works almost perfectly, except for my issue above.

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] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.