Support

Account

Home Forums Backend Issues (wp-admin) Not applying update field (acf) in my plugin

Solving

Not applying update field (acf) in my plugin

  • Hello
    This is my plugin
    Everything works properly and complete information is obtained But it is not updated by ACF
    That’s mean
    The data is correct up to the beginning of the acfs, but the value is not shown and cannot be set at all
    Thanks for helping me

  • sorry guys this is my code

    
    function unique_auto_add_vin_meta_box() {
        add_meta_box('unique-auto-vin-meta-box', 'Vehicle VIN', 'unique_auto_vin_meta_box_callback', 'post', 'side', 'high');
    }
    add_action('add_meta_boxes', 'unique_auto_add_vin_meta_box');
    
    function unique_auto_vin_meta_box_callback($post) {
        wp_nonce_field('unique_auto_save_vin_data', 'unique_auto_vin_meta_box_nonce');
    
        $vin = get_post_meta($post->ID, '_unique_auto_vin', true);
    
        echo '<label for="unique_auto_vin">Enter VIN:</label>';
        echo '<input type="text" id="unique_auto_vin" name="unique_auto_vin" value="' . esc_attr($vin) . '" size="25" />';
        echo '<button type="button" id="fetch_vin_data">Fetch Data</button>';
    
        ?>
        <script type="text/javascript">
        jQuery(document).ready(function($) {
            $('#fetch_vin_data').click(function() {
                var vin = $('#unique_auto_vin').val();
                var postID = <?php echo $post->ID; ?>;
                var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
    
                $.ajax({
                    url: ajaxurl,
                    type: 'POST',
                    data: {
                        'action': 'fetch_vin_data',
                        'vin': vin,
                        'postID': postID,
                        'nonce': '<?php echo wp_create_nonce('unique-auto-fetch-vin'); ?>'
                    },
                    
                    success: function(response) {
                        alert('Data fetched successfully!');
                    },
                    error: function(error) {
                        alert('Failed to fetch data');
                    }
                });
                
            });
        });
        </script>
        <?php
    }
    
    function unique_auto_save_vin_data($post_id) {
        if (!isset($_POST['unique_auto_vin_meta_box_nonce']) || !wp_verify_nonce($_POST['unique_auto_vin_meta_box_nonce'], 'unique_auto_save_vin_data')) {
            return;
        }
        if (!current_user_can('edit_post', $post_id)) {
            return;
        }
        if (isset($_POST['unique_auto_vin'])) {
            update_post_meta($post_id, '_unique_auto_vin', sanitize_text_field($_POST['unique_auto_vin']));
        }
    }
    add_action('save_post', 'unique_auto_save_vin_data');
    
    function unique_auto_fetch_vin_data() {
        check_ajax_referer('unique-auto-fetch-vin', 'nonce');
    
        $vin = isset($_POST['vin']) ? sanitize_text_field($_POST['vin']) : '';
        $postID = isset($_POST['postID']) ? intval($_POST['postID']) : 0;
        if (!empty($vin) && !empty($postID)) {
            $response = wp_remote_get("https://auto.dev/api/vin/$vin?apikey=ZrQEPSkKbS5vdmVpc3NpNjlAZ21haWwuY29t");
    
            if (is_wp_error($response)) {
                error_log('API request error: ' . $response->get_error_message());
                wp_send_json_error('API request failed');
            }
    
            $body = wp_remote_retrieve_body($response);
            $data = json_decode($body, true);
    
            if (isset($data['years']['0']['year'], $data['make']['name'], $data['model']['name'])) {
                update_field('year',  $data['years']['0']['year'], $postID);
                update_field('make', $data['make']['name'], $postID);
                update_field('model', $data['model']['name'], $postID);
    
                wp_send_json_success('Vehicle data updated successfully.');
            } else {
                wp_send_json_error('Required vehicle data not found in the API response.');
            }
        } else {
            wp_send_json_error('Invalid VIN or Post ID');
        }
    
        wp_die();
    }
    add_action('wp_ajax_fetch_vin_data', 'unique_auto_fetch_vin_data');
  • We’re seeing a similar issue when calling update_post_meta() or the ACF update_field() function from /wp-admin/post.php

    If I delete any records from the database related to a given wp_postmeta field, then update_field() will work properly, but if I call it repeatedly to update a post’s meta data then it will retain the previous value.

    I sanity-checked it by deactivating the ACF Pro plugin, and then it worked as expected. When I reactivated the plugin, the problem returned.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.