WordPress

ajax — Nie stosuje się pola aktualizacji (acf) w mojej wtyczce

  • 26 lutego, 2024
  • 3 min read
ajax — Nie stosuje się pola aktualizacji (acf) w mojej wtyczce


to jest mój kod wtyczki

<?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("

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

Wszystko działa prawidłowo i uzyskiwane są pełne informacje. Jednak ACF nie aktualizuje ich. Dziękuję za pomoc

Warto przeczytać!  backbone — Używanie klienta javascript REST API (v2) na prywatnej trasie z przestrzenią nazw


Źródło