admin管理员组

文章数量:1122826

I'm trying to update option_name values from my form fields on form submission

// Update value from wp_options 'oxymade_colors' name (--primary-color)

// Used to update the rgb color

$color_mappings = array(
    'field_1047' => '--primary-color',
    'field_1088' => '--secondary-color',
);

// Retrieve the entire array from the database
$oxymade_colors = get_option('oxymade_colors');

// Check if the array is valid and contains the 'colors' key
if (is_array($oxymade_colors) && isset($oxymade_colors['colors'])) {
    // Loop through the field mappings
    foreach ($color_mappings as $field_id => $option_name) {
        // Get the submitted value for the current field
        $new_color_value = isset($submit->meta[$field_id]['value']) ? $submit->meta[$field_id]['value'] : '';

        // Loop through the 'colors' array to find the current option name
        foreach ($oxymade_colors['colors'] as &$color) {
            if (isset($color['name']) && $color['name'] === $option_name) {
                // Update the value
                $color['value'] = $new_color_value;
                break;
            }
        }
    }

    // Update the entire array in the database
    update_option('oxymade_colors', $oxymade_colors);
  }
}

Everything works as expected and I'm able to update the right name in the array. Also I can see the new value in the database and in the plugin settings.

However these changes are not reflecting in the frontend. I need to go to the plugin settings, only press the update button and magically (although nothing changed in the database), I can see the result.

I'm not sure what I'm missing since the database is updated correctly. Any action missing?

本文标签: functionsHow to trigger an updateoption