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

  • 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.

  • 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 );
    
  • 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.

  • 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. 🙂

  • Thank you @jhned for sharing your solution!

  • 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,

  • @jhned wow this is awesome! Thank you!

    Would this also pull in layouts that contain repeater fields and all it’s data?

  • this seems to be what I want to be able to do, but I don’t understand how the select field is being implemented. In the field group with the flex-content? What is the select box populated with. Seems like a ‘post object’ field would be called for (tried that with no success).

    I’m sure I am missing something here but…

    the WordPress repository plugin blew up my page.

  • @jhned: great piece of code! Thanks for sharing!


    @johnnya23
    : I made a screenshot of the settings. You have to replace the field keys (“Schlüssel”). To see them go to configure view on the top right and activate field keys.
    The select field is a Post-Object field which returns the id of the selected post. Multiple selection is active.

    In my example the layouts field is called layout. So you have to replace “layouts” to “layout” here:

    // Get the layouts value from the selected page.
    $layouts_from_page = get_field_object( ‘layouts’, $page_id, false, true );

    In Gutenberg you have to reload the page after saving to see the copied fields.

    Full resolution image

    Settings

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

The topic ‘Copy Flexible Content Layout From One Post to Another’ is closed to new replies.