Last active
September 20, 2024 16:30
-
-
Save champsupertramp/c1f6d83406e9e0425e9e98aaa36fed7d to your computer and use it in GitHub Desktop.
Ultimate Member - adding custom fields in account page and tab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
/** 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
`<?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"
];
}`
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