Support

Account

Forum Replies Created

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

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

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

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