Support

Account

Home Forums Add-ons Flexible Content Field Save flexible content rows in post content

Solved

Save flexible content rows in post content

  • Hi everyone,

    I wish to ask if someone know the correct way on how to save all rows of a flexible content in the post “content”, possibly when a user click the “save/publish post” button.

    I’m using the flexible content field as a page builder and I’ve hidden the default wordpress tinymce editor.

    My poupose is to save the whole content (all fields within each row) of the flexible field inside the post content. I suppose that I’ve to loop through all flexible rows and perform a merging of the rows content.

    I’m not sure if using acf/save_post is the right method. Anyone has some suggestions to send me in the right way?
    I’m also considering that saving a post content during the save_post action could bring in a sort of infinite loop.

    Thank you very much in advance.

  • The question I have is why do you want to put the values of the fields into post_content?

    and do you want this content to be formatted for display?

  • Hi John,

    the main reason is for the search function: when a user perform a search within a website the search functionality looks in the post_title and post_content.
    I don’t know if is there a way to search also within a flexible content fields.

    Another reason could be the export functionality. This way you can export all posts for using it in another website that has not ACF plugin installed.

    Maybe another reason could be the REST API functionality, but currently I’m not sure about this.

    Let’s say that, in specific cases could be useful having a method for merging and putting flexible content fields in the post content.

    In my case the flexible content is used as page builder and replace completly the post content editor.

    Anyway, actually I don’t need to display this content.

  • If you want to move content to post_content for search it is very different than if you want to move content that can be displayed on another site.

    For search, all you need is the data of the field to be in the content and not formatted in any way. On the other hand, importing it into a site without ACF and being able to use it on another site without editing is completely different.

    The first is easier than the second.

    For the second you will need to loop through all of the fields when saved in the same way that you display the pages/posts on the front end of the site. You need to build all the html and content and then insert it into the post. This will be like building the front end of the site twice. More than likely this process will time out the admin of your site when saving posts if they have a large number of fields.

    There are two methods for the first, you could do the looping and put all the content into the content. This first method will have the same problems as trying to build the html content. The second method is to directly query the database to get all of the meta content from the post in just a few queries and then insert it. This is faster, but more complicated.

    Something else that you need to deal with is the fact that flex fields change and ACF does not update removed fields. Looping through the content and building it the same way as is done for the front end of the site avoids putting this stale data into the content. But then your back to dealing with the time involved to do the work. On the other hand, doing DB queries means that you need to find a way to remove that stale data or ignore it so that it’s not inserted into the content.

    Anyway, those are pretty much your options and I wanted you to have those before I gave a link to a plugin. This is a plugin that I’m working on to deal with the search issue and it is not meant to deal with the formatting issue in any way. It also only deals with text, textarea and wysiwig fields at this time because it’s all I am really concerned about. I probably won’t add other types of fields unless I have a need to. I feel that these 3 field types added to content will resolve 90% of the search problem. You are free to try it out or to look at my code to see how I’m doing this. It uses the second method of doing direct DB queries. I’ll let you read the rest there https://github.com/Hube2/acf-to-content

  • Hi John,
    thank you very much for the clear and huge explanation.

    At the moment the important task is to solve the question of putting the flexible content into the post_content.

    Actually I’m using the following solution that works very well for me. This solution is designed only for my pourposes but could be customized for other possible scenarios.

    function flexible_content_to_post_content( $post_id ) {
    
    	$post_type = 'books';
    
    	//Check if we are saving a books post type
    	if( get_post_type( $post_id ) != $post_type)
    		return;
    
    	//Check it's not an auto save routine
    	if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
    		return;
    
    	//The Post Content
    	$post_content = '';
    	
    	//Loop the flexible content rows
    	if( have_rows('flexible_content') ):
    		while ( have_rows('flexible_content') ) : the_row();
    		
    			//TEXT BLOCK
    			if( get_row_layout() == 'text_block' ):
    				
    				$post_content .= get_sub_field('wysiwyg');
    	
    			endif;
    				
    		endwhile;
    	endif;
    	
    	
        //If calling wp_update_post, unhook this function so it doesn't loop infinitely
        remove_action('save_post', 'flexible_content_to_post_content');
    
    	// call wp_update_post update, which calls save_post again. E.g:
        wp_update_post(array('ID' => $post_id, 'post_content' => $post_content));
    
        // re-hook this function
        add_action('save_post', 'flexible_content_to_post_content');
    	
    }
    add_action('save_post', 'flexible_content_to_post_content');

    Thanks for sharing your plugin. At the moment the solution I’ve reported above is enough easy to use for me. I don’t need to check for all acf fields within the post. I need only to check text fields (textinput, textarea, wysiwyg) inside the flexible content.

    Of course I need to do some testing for large flexible fields, but considering my personal context, this should not be a problem.

    Thank you very much.

  • That is pretty much the way I was doing this for sites I’ve built, but I had an issue with saving timing out when the number of layouts and wysiwyg fields grew too large on complex site that I built recently. If you’re content is limited then your solution is probably the best in most cases.

  • Which is the max number of layouts that give you issues with timeout?

    I suppose we should consider even that the server timeout could be relative also to the hosting solution.
    Currently, I’m working on a dedicated server, so maybe for me, this problem could be more limited.

  • The timeouts actually have to do with the browser. >30 seconds and browsers will generally show a timeout message. This can be an issue with ACF when there are a lot of fields to be saved without adding any overhead for adding the content to post_content. This is purely a case of the number of queries involved because WP only updates one meta value at a time and each update can take 2 to 4 db queries to complete. The first time I saw this issue was on a site where each layout had up to 7 wysiwyg fields. This site would time out just saving ACF data (without the extra processing) at around 10 to 15 layouts (as many as 135 wysiwyg editors), depending on how much content was being saved.

    Just a note that the timeout in this case will not cause the data to not be saved. Since there is no communication with the browser until php completes all the saving it completes before it knows that the browser has disconnected. When this happens you simply need to go back to the admin page and reload it to see all the changes. I supposed, it really extreme cases PHP could also timeout, depending on what your max execution time is, and that could cause an issue with the data actually being saved.

  • Thanks for the support John, I’m marking this topic as resolved.

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

The topic ‘Save flexible content rows in post content’ is closed to new replies.