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