Skip to content

Instantly share code, notes, and snippets.

@ayoubkhan58
Last active February 24, 2022 07:03
Show Gist options
  • Save ayoubkhan58/6e93715cb61c7998fc03215486161c79 to your computer and use it in GitHub Desktop.
Save ayoubkhan58/6e93715cb61c7998fc03215486161c79 to your computer and use it in GitHub Desktop.
Ultimate Member - adding custom fields in account page and tab, add one of the code to functions.php
/**
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);
/*
* Add custom fields to UM Account general
*
* @param int $user_id
* @param array $changes
*/
function wpkj_um_after_account_general() {
$custom_fields = [
'user_country' => __('Country'),
'cnic_if_pakistani' => __('CNIC (If Pakistani)'),
'complete_address' => __('Complete Address'),
];
$custom_fields_freelance_profiles = [
'fiverr_profile_link' => __('Fiverr Profile'),
'upwork_profile_link' => __('Upwork Profile'),
'guru_profile_link' => __('Guru Profile'),
'freelancer_profile_link' => __('Freelancer Profile'),
'peopleperhour_profile_link' => __('PeoplePerHour Profile')
];
$custom_fields_social = [
'facebook_profile_link' => __('Facebook Profile'),
'twitter_profile_link' => __('Twitter Profile'),
'linkedin_profile_link' => __('LinkedIn Profile'),
'youtube_profile_link' => __('YouTube Profile'),
'github_profile_link' => __('GitHub Profile'),
'instagram_profile_link' => __('Instagram Profile')
];
foreach ($custom_fields as $key => $value) {
$fields[ $key ] = array(
'title' => $value,
'metakey' => $key,
'type' => 'text',
'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;
}
$html_label_1 = "<br><br><h4 class=''>Social Media Links</h4>";
echo $html_label_1;
foreach ($custom_fields_social as $key => $value) {
$fields[ $key ] = array(
'title' => $value,
'metakey' => $key,
'type' => 'text',
'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;
}
$html_label_2 = "<br><br><h4 class=''>Freelancer Profile Links</h4>";
echo $html_label_2;
foreach ($custom_fields_freelance_profiles as $key => $value) {
$fields[ $key ] = array(
'title' => $value,
'metakey' => $key,
'type' => 'text',
'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;
}
}
add_action('um_after_account_general', 'wpkj_um_after_account_general', 100);
/**
* Save custom fields
*
* @param int $user_id
* @param array $changes
*/
function wpkj_um_after_user_account_updated( $user_id, $changes ) {
if( isset( $_POST[ 'user_country' ] ) && !UM()->form()->has_error( 'user_country' ) ) {
$user_country = sanitize_text_field($_POST[ 'user_country' ]);
// Update the Nickname field
update_user_meta( $user_id, 'user_country', $user_country );
// Update the "Public Display As" field
// wp_update_user( array( 'ID' => $user_id, 'display_name' => $devplus ) );
}
if( isset( $_POST[ 'cnic_if_pakistani' ] ) && !UM()->form()->has_error( 'cnic_if_pakistani' ) ) {
$cnic_if_pakistani = sanitize_text_field($_POST[ 'cnic_if_pakistani' ]);
// Update the Nickname field
update_user_meta( $user_id, 'cnic_if_pakistani', $cnic_if_pakistani );
}
if( isset( $_POST[ 'complete_address' ] ) && !UM()->form()->has_error( 'complete_address' ) ) {
$complete_address = sanitize_text_field($_POST[ 'complete_address' ]);
// Update the Nickname field
update_user_meta( $user_id, 'complete_address', $complete_address );
}
if( isset( $_POST[ 'fiverr_profile_link' ] ) && !UM()->form()->has_error( 'fiverr_profile_link' ) ) {
$fiverr_profile_link = sanitize_text_field($_POST[ 'fiverr_profile_link' ]);
// Update the Nickname field
update_user_meta( $user_id, 'fiverr_profile_link', $fiverr_profile_link );
}
if( isset( $_POST[ 'upwork_profile_link' ] ) && !UM()->form()->has_error( 'upwork_profile_link' ) ) {
$upwork_profile_link = sanitize_text_field($_POST[ 'upwork_profile_link' ]);
// Update the Nickname field
update_user_meta( $user_id, 'upwork_profile_link', $upwork_profile_link );
}
if( isset( $_POST[ 'guru_profile_link' ] ) && !UM()->form()->has_error( 'guru_profile_link' ) ) {
$guru_profile_link = sanitize_text_field($_POST[ 'guru_profile_link' ]);
// Update the Nickname field
update_user_meta( $user_id, 'guru_profile_link', $guru_profile_link );
}
if( isset( $_POST[ 'freelancer_profile_link' ] ) && !UM()->form()->has_error( 'freelancer_profile_link' ) ) {
$freelancer_profile_link = sanitize_text_field($_POST[ 'freelancer_profile_link' ]);
// Update the Nickname field
update_user_meta( $user_id, 'freelancer_profile_link', $freelancer_profile_link );
}
if( isset( $_POST[ 'peopleperhour_profile_link' ] ) && !UM()->form()->has_error( 'peopleperhour_profile_link' ) ) {
$peopleperhour_profile_link = sanitize_text_field($_POST[ 'peopleperhour_profile_link' ]);
// Update the Nickname field
update_user_meta( $user_id, 'peopleperhour_profile_link', $peopleperhour_profile_link );
}
if( isset( $_POST[ 'facebook_profile_link' ] ) && !UM()->form()->has_error( 'facebook_profile_link' ) ) {
$facebook_profile_link = sanitize_text_field($_POST[ 'facebook_profile_link' ]);
// Update the Nickname field
update_user_meta( $user_id, 'facebook_profile_link', $facebook_profile_link );
}
if( isset( $_POST[ 'twitter_profile_link' ] ) && !UM()->form()->has_error( 'twitter_profile_link' ) ) {
$twitter_profile_link = sanitize_text_field($_POST[ 'twitter_profile_link' ]);
// Update the Nickname field
update_user_meta( $user_id, 'twitter_profile_link', $twitter_profile_link );
}
if( isset( $_POST[ 'linkedin_profile_link' ] ) && !UM()->form()->has_error( 'linkedin_profile_link' ) ) {
$linkedin_profile_link = sanitize_text_field($_POST[ 'linkedin_profile_link' ]);
// Update the Nickname field
update_user_meta( $user_id, 'linkedin_profile_link', $linkedin_profile_link );
}
if( isset( $_POST[ 'youtube_profile_link' ] ) && !UM()->form()->has_error( 'youtube_profile_link' ) ) {
$youtube_profile_link = sanitize_text_field($_POST[ 'youtube_profile_link' ]);
// Update the Nickname field
update_user_meta( $user_id, 'youtube_profile_link', $youtube_profile_link );
}
if( isset( $_POST[ 'github_profile_link' ] ) && !UM()->form()->has_error( 'github_profile_link' ) ) {
$github_profile_link = sanitize_text_field($_POST[ 'github_profile_link' ]);
// Update the Nickname field
update_user_meta( $user_id, 'github_profile_link', $github_profile_link );
}
if( isset( $_POST[ 'instagram_profile_link' ] ) && !UM()->form()->has_error( 'instagram_profile_link' ) ) {
$instagram_profile_link = sanitize_text_field($_POST[ 'instagram_profile_link' ]);
// Update the Nickname field
update_user_meta( $user_id, 'instagram_profile_link', $instagram_profile_link );
}
}
add_action( 'um_after_user_account_updated', 'wpkj_um_after_user_account_updated', 20, 2 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment