So I am trying to find things to do in order to speed up my website. As I mainly have logged in users (membership site), I am looking into fragment caching and as I am lazy I want to fragment cache as large portions as possible.
Good to know is I am no expert at all in caching and I am just starting to look into this. But my goal is to cache my “page editor” output on each page in order to speed up every single page. I use the code found below in order to do this.
Is this a good solution, or is it bad? If so why, and what can I do instead in order to speed up the website?
What I am doing is basically I save the whole html output from a flexible content field and save it in a transient (I also delete the transient with acf/save_field when I update a page).
<?php
$page_editor_cache = get_transient('page_editor_cache_' . get_the_ID());
if ( false === $page_editor_cache ):
ob_start();
if( have_rows('page_editor') ):
// loop through the rows of data
while ( have_rows('page_editor') ) : the_row();
if( get_row_layout() == 'hero_masonry' ):
get_template_part('template-parts/components/acf/acf', 'hero_masonry');
elseif( get_row_layout() == 'social_stream_display' ):
get_template_part('template-parts/acf-parts/acf', 'social_stream_display');
endif;
endwhile;
else :
endif;
$page_editor_cache = ob_get_clean();
set_transient( 'page_editor_cache_' . get_the_ID(), $page_editor_cache, DAY_IN_SECONDS );
endif;
echo $page_editor_cache;
?>
There isn’t any reason for you to build your own fragment cache, there are several already available as plugins. https://wordpress.org/plugins/search.php?q=fragment+cache. There is one built by me in there, but I won’t plug just that one. If you did want to build you’re own fragment cache many of the plugins in that list wold be a good place to start looking at how others have done it. There are also several good tutorials on creating a fragment cache for WordPress https://www.google.com/search?q=wordpress+fragment+caching&ie=utf-8&oe=utf-8.