Support

Account

Home Forums Add-ons Flexible Content Field Copy Flexible Content Layout From One Post to Another

Solving

Copy Flexible Content Layout From One Post to Another

    • jhned

    • July 23, 2016 at 1:48 am

    I wish there were a way to save configurations of a Flexible Content layout. For instance, I have a “Slideshow” layout. If I need to re-use the same Flexible Content slideshow layout from the Home page on the About page of a site, I have to copy all of my content and settings from one post to the other.

    It’d be nice if you could copy an ACF Flexible Content layout from one post to another. It’s been requested before here: https://support.advancedcustomfields.com/forums/topic/copy-flexible-content-field/.

    I think I’m going to take a look at the ACF Actions to see if there’s anything I can use there to import data from another post. If there’s anything you could point me to, I’d really appreciate the help.

    • jhned

    • July 23, 2016 at 3:54 am

    To answer my own question, here’s an example of how I achieved it with a Select field, Flexible Content field, and a function that runs on acf/save_post:

    
    function import_layouts_from_a_different_page( $post_id ) {
    
    	// Bail early if no ACF data
    	if ( empty( $_POST['acf'] ) ) {
    		return;
    	}
    
    	// Flex layouts from this page (field_56381b4c8e557 is a Flexible Content field named "Layouts")
    	if ( is_array( $_POST['acf']['field_56381b4c8e557'] ) && ! empty( $_POST['acf']['field_56381b4c8e557'] ) ) {
    		$current_page_flex_layouts = $_POST['acf']['field_56381b4c8e557'];
    	} else {
    		$current_page_flex_layouts = array();
    	}
    
    	// Determine if there are any pages to import (field_57924308049be is a Select field named "import_flex_layout")
    	$pages_to_import = $_POST['acf']['field_57924308049be'];
    
    	// If there aren't any layouts to import, skip the rest.
    	if ( empty( $pages_to_import ) ) {
    		return;
    	}
    
    	// Loop through the (possibly) multiple pages that we'll import.
    	foreach ( $pages_to_import as $page_id ) {
    
    		// Get the layouts value from the selected page.
    		$layouts_from_page = get_field_object( 'layouts', $page_id, false, true );
    
    		// Add the value to this page.
    		if ( ! empty( $layouts_from_page['value'] ) ) {
    			$current_page_flex_layouts = array_merge( $current_page_flex_layouts, $layouts_from_page['value'] );
    		}
    	}
    
    	// Re-set the Layout field value with any imported pages, then continue saving.
    	$_POST['acf']['field_56381b4c8e557'] = $current_page_flex_layouts;
    
    	// Clear out the pages to import setting
    	$_POST['acf']['field_57924308049be'] = array();
    
    }
    
    // run before ACF saves the $_POST['acf'] data
    add_action( 'acf/save_post', 'import_layouts_from_a_different_page', 1 );
    
    • James

    • July 25, 2016 at 3:51 pm

    Hi @jhned

    I think your method is the best way to do it. You can also use the update_field() function and use acf/save_post with priority of 20 instead.

    Thanks 🙂

  • Your solution is perfect and worked great for what I needed!

    For anyone who wants to use, I also added a toggle option that would show the Page Link dropdown after the user agrees to the import process. Make sure you clear both of those fields after import, if you go that route.

  • @failcookie I have been looking for a solution to a similar problem and was wondering if you could share your code with the addition of the dropdown. Thank you.

    • Gleb M

    • June 19, 2017 at 7:45 pm

    Take a look at this plugin: https://github.com/infazz/flexible-templates
    It allows saving of Flexible content templates.
    Please note that current version is only able to work with one flexible content on the page. Plugin is fresh and in beta. 🙂

    • Alain O

    • November 1, 2017 at 8:11 am

    Thank you @jhned for sharing your solution!

    • andreu

    • October 10, 2018 at 6:24 am

    You can also check this new plugin https://wordpress.org/plugins/acf-flexible-layouts-manager/ which is really awesome and do exactly what you need, I guess.
    Best,

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.