Custom Fields in Account Page are not saving upon Update Account
Hello. I am new to WordPress and I am trying to create a userbase with varied shipping details – depending whether a user is a resident of a community or not. I have successfully added the custom fields and the conditions after 5 hours of trying, but unfortunately when I click Update Account – they do not save. Can anyone advise me on how to change the functions.php or my custom .js for this to occur? I feel like I have tried everything.
functions.php
/**
* Enqueue parent theme styles and child theme styles.
*/
function enqueue_parent_and_child_styles() {
// Enqueue parent theme styles
wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css');// Enqueue WooCommerce stylesheet from parent theme, if exists
if (class_exists('WooCommerce')) {
wp_enqueue_style('woocommerce-style', get_template_directory_uri() . '/woocommerce.css', array('parent-style'));
}
// Enqueue child theme styles
wp_enqueue_style('child-style', get_stylesheet_uri(), array('parent-style'));
}
add_action('wp_enqueue_scripts', 'enqueue_parent_and_child_styles');
/* Add fields to account page */
add_action('um_after_account_general', 'showExtraFields', 100);
function showExtraFields()
{
$custom_fields = array(
"phone_number" => "Phone Number",
"jamiitown_resident_confirm" => "Are you a Jamiitown resident?",
"jamiitown_project_select" => "Jamiitown Project",
"jamiitown_house_number" => "Jamiitown House Number",
"country" => "Country",
"state_county" => "State / County",
"town_city" => "Town / City",
"postcode_zip" => "Postcode / Zip",
"street_address" => "Street Address"
);
foreach ($custom_fields as $key => $value) {
$field_value = get_user_meta(get_current_user_id(), $key, true) ?: '';
switch ($key) {
case 'jamiitown_resident_confirm':
$html="
';
break;case 'jamiitown_project_select':
$html="
';
break;default:
$html="
type="text" name="' . $key . '"
id="' . $key . '" value="' . esc_attr($field_value) . '"
placeholder=""
data-validate="" data-key="' . $key . '">
';
break;
}echo $html;
}
}
// Enqueue the custom JavaScript file
function enqueue_custom_account_fields_script() {
wp_enqueue_script(
'custom-account-fields',
get_stylesheet_directory_uri() . '/js/custom-account-fields.js',
array('jquery'),
null,
true
);
// Localize script to pass variables to JavaScript
wp_localize_script('custom-account-fields', 'um_fields_data', array(
'ajax_url' => admin_url('admin-ajax.php'),
));
}
add_action('wp_enqueue_scripts', 'enqueue_custom_account_fields_script');
// Save custom fields
add_action('um_account_pre_update', 'saveExtraFields', 100);
function saveExtraFields($user_id)
{
$custom_fields = array(
"phone_number",
"jamiitown_resident_confirm",
"jamiitown_project_select",
"jamiitown_house_number",
"country",
"state_county",
"town_city",
"postcode_zip",
"street_address"
);
foreach ($custom_fields as $key) {
if (isset($_POST[$key])) {
update_user_meta($user_id, $key, sanitize_text_field($_POST[$key]));
}
}
}
custom-account-fields.js
jQuery(function($) {
// Function to toggle fields based on jamiitown_resident_confirm value
function toggleFields() {
var jamiitownResidentValue = $('#jamiitown_resident_confirm').val();
console.log('jamiitownResidentValue:', jamiitownResidentValue);if (jamiitownResidentValue === 'yes') {
$('.um-field-country, .um-field-state_county, .um-field-town_city, .um-field-postcode_zip, .um-field-street_address').hide();
$('.um-field-jamiitown_project_select, .um-field-jamiitown_house_number').show();
} else if (jamiitownResidentValue === 'no') {
$('.um-field-country, .um-field-state_county, .um-field-town_city, .um-field-postcode_zip, .um-field-street_address').show();
$('.um-field-jamiitown_project_select, .um-field-jamiitown_house_number').hide();
}
}
// Initial call to toggle fields on page load
toggleFields();
// Event listener for change in jamiitown_resident_confirm field
$('#jamiitown_resident_confirm').change(function() {
toggleFields();
});
});
All help will be greatly appreciated!
مشاهده پاسخ های این مطلب
———————————————
این مطلب از سایت انجمن وردپرس گردآوری شده است و کلیه حقوق مطلق به انجمن وردپرس می باشد در صورت مغایرت و یا بروز مشکل اطلاع دهید تا حذف گردد
منبع: انجمن وردپرس