Created
June 7, 2012 23:53
-
-
Save RalfAlbert/2892482 to your computer and use it in GitHub Desktop.
Page template for WordPress with caching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'save_post', 'clear_many_sites_transient', 10, 1 ); | |
add_action( 'edit_post', 'clear_many_sites_transient', 10, 1 ); | |
function clear_many_sites_transient( $post_id ){ | |
$post = get_post( $post_id ); | |
if( 'page' === $post->post_type ) | |
delete_transient( 'many_sites_in_one' ); | |
return $post_id; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Template Name: Many sites in one (with caching) | |
*/ | |
// set expiration of transient | |
/* | |
* 86400 = 1 day | |
* 604800 = 1 week | |
* 2592000 = 1 month | |
*/ | |
$expiration = 86400; | |
// create filepath for caching file | |
$upload_dir = wp_upload_dir(); | |
$cache_file = $upload_dir['basedir'] . '/many_sites.cache'; | |
$is_cached = file_exists( $cache_file ) && is_readable( $cache_file ); | |
// get cache transient | |
if( ( TRUE === get_transient( 'many_sites_in_one' ) ) && $is_cached ){ | |
exit( file_get_contents( $cache_file ) ); | |
} else { | |
ob_start(); | |
/* | |
* create your site here | |
*/ | |
$content = ob_get_clean(); | |
$filehandle = fopen( $cache_file, 'w+' ); | |
if( $filehandle ){ | |
fwrite( $filehandle, $content ); | |
fclose( $filehandle ); | |
} | |
$is_cached = file_exists( $cache_file ) && is_readable( $cache_file ); | |
set_transient( 'many_sites_in_one', $is_cached, $expiration ); | |
echo $content; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment