How to: ACF Bulk data import for repeaters (ACF v6+)

We used CodeWP to generate a PHP function that imports comma-separated links from one ACF field into a repeater when saving a custom post type.
calendar_today
Published: January 31, 2023
Founder & CEO
AI Tools For WordPress Creators
Code, learn, troubleshoot, and secure WordPress sites with the CodeWP platform and our custom AI.
Start for Free
Contents

Earlier, I was tying to import a bulk list of links into an ACF repeater. There are several heavy import plugins that will get the job done, but I needed something simpler.

I used CodeWP to generate this code for my use case. When saving a specific post type, it'll check if I've added data to import, and if it's present, add it to the specified repeater. Quick and simple!

PHP
  //when post type is dataset and it's updated, run this function (only on the admin backend post edit page)
add_action('acf/save_post', 'urlimporter', 20);
function urlimporter()
{
    //if import_links acf field has content, and it's comma separated (https://url1.com,https://url2.com), get each item
    //for each link (use foreach), create a new repeater row for 'dataset' repeater, then add the link to the 'original_link' subfield
    //on success, delete the import_links field

    if (get_field('import_links')) {
        $post_id = get_the_ID();
        $repeater_name = 'dataset';
        $import_links = get_field('import_links');
        $import_links = explode(",", $import_links);
        foreach ($import_links as $link) {
            add_row($repeater_name, array('original_link' => $link), $post_id);
        }
        update_field('import_links', '', $post_id);
    }
}

Here's how it works.

We have two fields set using ACF for our custom post type. Import links, and the repeater.

If the user saves the post, and there is a common separated, list of links in the import links field, the function will run separating out each link and then adding it as a new repeater role. It will also clear the field.

It uses the acf/save_post action, which fires when saving the submitted $_POST data.

This function is a quick and simple way to insert large amounts of data into repeaters and it's easily extendable using CodeWP.

About The Author
James LePage
Founder & CEO
More By James LePage
James LePage is an entrepreneur and founder of CodeWP, an AI platform for WordPress creators. With nearly a decade of experience founding startups and leading development agencies, James is focused on leveraging AI research and natural language processing to build smart solutions that make WordPress website creation efficient. As founder of CodeWP, James is dedicated to eliminating tedious developer searches and expensive hiring so anyone can build complex WordPress solutions easily. His technical expertise and passion for innovation drive CodeWP’s mission to serve WordPress creators globally with advanced AI capabilities.
More
Tags
ACF

More Reading

Make WordPress Easy With AI
© WPAI, Inc. 2024 | d.b.a CodeWP