Insert Update Acf image repeater field programmatically

This tutorial explains how to insert update Acf image repeater field programmatically. Here it shows how to read images from the external folder and add those images to the acf repeater image field.

Please also refer the tutorial that explains how create and update wordpress post using an external script.The data read from the CSV and create wordpress post dynamically.


Insert_Update_Acf_image_repeater_field_programmatically

The script reads images from the above folder and upload the images to the media library and then it assigns the images to the acf repeater field.

Step by step explanation

  • Read all images from folder
  • Upload Image to wordpress media library
  • Insert Update Acf image repeater field programmatically

Read all images from folder

The following code read all the files from the folder. Click here for detailed explanation.

function readDataFromImageFolder() {
    
    $imageFolderName = 14;
    $base = dirname(__FILE__);
    $dirname = $base.DS.'images'.DS.$imageFolderName.DS;
    $files = array();
    
    if (!file_exists($dirname)) {
        echo "The directory $dirname not exists.".PHP_EOL;
        exit;
    } else {
        echo "The directory $dirname exists.".PHP_EOL;
        $dh  = opendir( $dirname );
        
        while (false !== ($filename = readdir($dh))) {
            if ($filename === '.' || $filename === '..') continue;
            $files[] = $dirname.$filename;
        }
        uploadImages( $files );
    }

  //  echo '<pre>';
  //  print_r($files);die();

}

Insert Update Acf image repeater field programmatically

The following code show how to assign the uploaded images to the acf repeater.

function uploadImages( $files ) {

    $imgAcfIds = array();

    foreach($files as $imgfile) {
        $filename = basename($imgfile);
        $upload_file = wp_upload_bits($filename, null, file_get_contents($imgfile));
        if (!$upload_file['error']) {
            $wp_filetype = wp_check_filetype($filename, null );
            $attachment = array(
                'post_mime_type' => $wp_filetype['type'],
                'post_parent' => 251,
                'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
                'post_content' => '',
                'post_status' => 'inherit'
            );
            $attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], 251 );

            // get attachment id for acf field

            $imgAcfIds[] = array('product_image' => $attachment_id);
            echo 'Attachment Id :'.$attachment_id .PHP_EOL;
            if (!is_wp_error($attachment_id)) {
                require_once(ABSPATH . "wp-admin" . '/includes/image.php');
                $attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] );
                wp_update_attachment_metadata( $attachment_id,  $attachment_data );
            }
        }
    }
    //echo '<pre>';
    //print_r($imgAcfIds);
    update_field( 'field_5985046a78782', $imgAcfIds , 251 );
}

upload_images_to_wordpress_media_library

upload_images_to_wordpress_media_library_assign_to_acf_repeater

Share this Tutorial

3 comments on “Insert Update Acf image repeater field programmatically

Leave a Reply

Your email address will not be published. Required fields are marked *

*