Support

Account

Home Forums Add-ons Flexible Content Field WordPress Editor as Flexible Content Layout

Solving

WordPress Editor as Flexible Content Layout

  • So… here’s my scenario.

    I have existing content – pages and posts. I want to create a page template that uses the flexible content field. But I want the default wordpress editor to be one of the layouts I set up for my flexible content field. When I switch page templates to my flexible content template I want the wordpress editor to automatically populate the flexible content field on pages that already exist on my site. Any thoughts on how to accomplish this?

    My first thought was to cheat it in with a true/false field, and use the default editor as is but… I’d really like to get the editor in the flexible content layouts and drag and drop it like the other layouts.

    I’ve been doing trial and error with this:

    function my_acf_save_content( $value, $post_id, $field )
    {
    	
    	
    $field_key = "field_52ec14e6c4e5d";
    $value = get_field($field_key);
    print_r($value);
    $page = get_page();
    $wp_content = $page->post_content;
    
    if(array_key_exists ( 'acf_fc_layout', $value)) :
    
    $value[] = array("wyz_one" => 'moo cow', "acf_fc_layout" => "layout_one");
    update_field( $field_key, $value, $post_id );
    
    else :
    
    $value[] = array("wyz_one" => $wp_content, "acf_fc_layout" => "layout_one");
    update_field( $field_key, $value, $post_id );
    
    endif;
    
    }
    add_action('save_post', 'my_acf_save_content');

    That is real rough. I’ve pulled together from other sources on this forum.

    Anywho, I’d appreciate any thoughts on this.

    – ben

  • Hi @benluoma

    Why not write a custom function that you run only once? Think of it as a ‘migration’ function.

    In this function you could query all posts, and loop over them. For each post, you can load in the flexible content field value, and append the post_content if not already added (somewhat similar to your above code).

    This would mean that you only need to run the function once, then comment it out and all your posts should be updated!

    Thanks
    E

  • Right. That’s not a bad idea. But… I was thinking more of a “code-less” solution that would work for those afraid to play around in the theme files.

    I keep going back to different variations of the above code. I’ve managed to get it somewhat working but my fields keep duplicating on save. I suck ;-(

    Anyway, thanks for the suggestion Elliot. I’ll keep plugging away for now and post my progress.

  • So I can thus far save my flexible content field layout to post_content with:

    //save field content to the_content
    function save_to_the_content( $value, $post_id )
    {
    		wp_update_post( array( 'ID' => $post_id, 'post_content' =>  $value ) );
    		return $value;
    }
    add_filter('acf/update_value/key=field_5318e88463d1e', 'save_to_the_content', 10, 3);

    I’m still struggling with getting it to work the opposite way – post_content to flexible content layout. I’m able to sort of accomplish it with this:

    //multi-dimensional array in_array
    function multi_in_array($value, $array) 
    { 
        foreach ($array as $item) 
        { 
            if (!is_array($item)) 
            { 
                if ($item == $value) 
                { 
                    return true; 
                } 
                continue; 
            } 
    
            if (in_array($value, $item)) 
            { 
                return true; 
            } 
            else if (multi_in_array($value, $item)) 
            { 
                return true; 
            } 
        } 
        return false; 
    } 
    
    function save_to_acf_layout( $value, $post_id, $field )
    {
    
    $field_key = "field_5318e86463d1d";
    $value = get_field($field_key);
    
    $page = get_post();
    $string = $page->post_content;
    
    if(!multi_in_array("the_content", $value) && $string != '') :
    	$value[] = array("text" => $string, "acf_fc_layout" => "the_content");
    	update_field( $field_key, $value, $post_id );
    endif;
    }
    
    add_action('save_post', 'save_to_acf_layout');
    

    I’m struggling to get both functions working together. And my second function is imperfect – doesn’t work exactly how I’d like. Thoughts?

  • So I’m almost there:

    //multi-dimensional array in_array
    function multi_in_array($value, $array) 
    { 
        foreach ($array as $item) 
        { 
            if (!is_array($item)) 
            { 
                if ($item == $value) 
                { 
                    return true; 
                } 
                continue; 
            } 
    
            if (in_array($value, $item)) 
            { 
                return true; 
            } 
            else if (multi_in_array($value, $item)) 
            { 
                return true; 
            } 
        } 
        return false; 
    } 
    
    function save_to_acf_layout( $content, $post_id )
    {
    
    $field_key = 'field_5318e86463d1d'; //flexible content key
    
    $value = get_field($field_key);
    
    if(get_page_template_slug( $post_id ) === '') :
    	if(!multi_in_array('the_content', $value)) :
    		$value[] = array('text' => $content, 'acf_fc_layout' => 'the_content');
    		update_field( $field_key, $value, $post_id );
    	else :
    		$value[0] = array('text' => $content, 'acf_fc_layout' => 'the_content');
    		update_field( $field_key, $value, $post_id );
    	endif;
    else :
    	$flex_content = $value[0]['text'];
    	wp_update_post( array( 'ID' => $post_id, 'post_content' =>  $flex_content ) );
    endif;
    
    return $content;
    }
    
    add_filter( 'content_edit_pre', 'save_to_acf_layout', 10, 2 );
    add_filter( 'content_save_pre', 'save_to_acf_layout', 10, 2 );

    But now I’m stuck with this infinite loop. I’ve tried removing the filters but when I do wp_update_post doesn’t run. Ideas?

  • Hi @benluoma

    I’m finding it difficult to understand your above code.
    Perhaps you will find the issue with your code and solve it by simplifying it by breaking it down into steps.
    Please add comments to each step to describe what you are doing along the way.

    Thanks
    E

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

The topic ‘WordPress Editor as Flexible Content Layout’ is closed to new replies.