0

this is my plugin code

<?php
/*
Plugin Name: Unique Auto VIN Fetcher
Description: Fetches vehicle data using VIN from a specified API and fills the data 
in custom fields in posts.
Version: 1.0
Author: Mohammad
*/


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');

Everything works properly and complete information is obtained But it is not updated by ACF Thank you for helping me

3
  • What do you mean not updated by ACF? You’re trying to configure unique_auto_vin on your custom posts but the field isn’t shown, or the values saved here aren’t shown? Or something else?
    – Rup
    Feb 27 at 1:48
  • @Rup Tnx for helping me The data is correct up to the beginning of the acfs, but the value is not shown and cannot be set at all Feb 27 at 8:22
  • You may want to open ticket with ACF. Feb 27 at 19:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.