• Resolved dreyko123

    (@dreyko123)


    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

    <?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 = '<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">
    <select class="um-form-field valid" name="' . $key . '" id="' . $key . '">
    <option value="yes"' . selected($field_value, 'yes', false) . '>Yes</option>
    <option value="no"' . selected($field_value, 'no', false) . '>No</option>
    </select>
    </div>
    </div>';
    break;

    case 'jamiitown_project_select':
    $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">
    <select class="um-form-field valid" name="' . $key . '" id="' . $key . '">
    <option value="origin"' . selected($field_value, 'origin', false) . '>Origin</option>
    <option value="oceanpark"' . selected($field_value, 'oceanpark', false) . '>Oceanpark</option>
    </select>
    </div>
    </div>';
    break;

    default:
    $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="' . esc_attr($field_value) . '"
    placeholder=""
    data-validate="" data-key="' . $key . '">
    </div>
    </div>';
    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!

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Fields in Account Page are not saving upon Update Account’ is closed to new replies.