WordPress

Wypełnianie pól obrazu ACF z pliku JSON

  • 22 kwietnia, 2021
  • 3 min read
Wypełnianie pól obrazu ACF z pliku JSON


Tak, istnieje możliwość wypełnienia pola galerii acf danymi json.

Załóżmy, że to jest plik JSON Twojej galerii z aplikacji…

{
    "gallery" : [
        "
        "
        "
        "
        "
    ]
}

Teraz w zadaniu cron możesz uruchomić funkcję pokazaną poniżej, aby obsłużyć aktualizacje pól obrazu galerii acf.

Poniższa przykładowa funkcja pobiera wszystkie istniejące obrazy galerii acf, zachowuje je i łączy nowe obrazy w polu galerii…

/**
 * @param $post_id int
 * @param $acf_gallery_field_name string
 * @param $json_url string
 */
function add_json_imgs_to_acf_gallery($post_id, $acf_gallery_field_name, $json_url) {

    // get the json data via url
    $json = file_get_contents($json_url);

    // decode json to array
    $array = json_decode($json,true);

    // if decoded json is array and has array key gallery (as per above json example)
    if (is_array($array) && array_key_exists('gallery')) {

        // lets begin multiple attachment array
        $attach_ids = [];

        // foreach of your json gallery urls
        foreach ($array['gallery'] as $img_url) {

            // check the type of file
            // we'll use this as the 'post_mime_type'
            $file_type = wp_check_filetype(basename($img_url),null);

            // get the path to the upload directory
            $wp_upload_dir = wp_upload_dir();

            // prepare an array of attachment post data
            $attachment = [
                'guid' => $wp_upload_dir['url'] . " . basename($img_url),
                'post_mime_type' => $file_type['type'],
                'post_title' => preg_replace('/\.[^.]+$/', '',basename($img_url)),
                'post_content' => '',
                'post_status' => 'inherit'
            ];

            // insert the attachment
            $attach_id = wp_insert_attachment($attachment,$img_url,$post_id);

            // make sure that this file is included, as wp_generate_attachment_metadata() depends on it
            require_once(ABSPATH . 'wp-admin/includes/image.php');

            // generate the metadata for the attachment, and update the database record
            $attach_data = wp_generate_attachment_metadata($attach_id,$img_url);
            wp_update_attachment_metadata($attach_id,$attach_data);

            // build attachment id's as an array
            $attach_ids[] = $attach_id;

        }

        // get our current acf gallery field images
        $gallery = get_field($acf_gallery_field_name,$post_id);

        // if we already have galley images
        if($gallery) {

            // empty array for existing gallery image id to be populated
            $existing_attach_ids = [];

            // for each gallery item as array key => image array
            foreach ($gallery as $key => $img) {

                // add image id to existing attachment ids 
                $existing_attach_ids[] = $img['ID'];

            }

            // merge existing acf gallery ids with newly json uploaded image items
            $attach_ids = array_merge($existing_attach_ids,$attach_ids);

            // update acf gallery field with merged attachment ids
            update_field($acf_gallery_field_name,$attach_ids,$post_id);

        } else {

            // update acf gallery field with new attachment ids
            update_field($acf_gallery_field_name,$attach_ids,$post_id);

        }

    }

}


Źródło

Warto przeczytać!  Jak poprawnie dodać suwak polecanej treści w WordPress