Skip to content

Instantly share code, notes, and snippets.

@byronrode
Created June 10, 2013 22:34
Show Gist options
  • Save byronrode/5753022 to your computer and use it in GitHub Desktop.
Save byronrode/5753022 to your computer and use it in GitHub Desktop.
Allows developer/client only customization previews with WordPress.
<?php
/*
*
* @author: Byron Rode
* @description: Allows developer/client only customization previews with WordPress. Uses a simple
* cookie to handle previews, so don't go showing sensitive data. PS. I am NOT responsible
* if you do.
* @license: Free as in Beer
*
*/
// This handles the cookie creation/deletion based on a query
// string variable (preview=true|false).
if( !function_exists( 'tgm_dev_preview' ) ) {
function tgm_dev_preview()
{
if( isset($_GET['preview']) && $_GET['preview'] == 'true' ) {
if( !isset($_COOKIE['tgm_show_preview']) || isset($_COOKIE['tgm_show_preview']) && $_COOKIE['tgm_show_preview'] == 'false' ) {
setcookie('tgm_show_preview', 'true', time() + 3600, '/');
}else{
setcookie('tgm_show_preview', '', time() - 3600, '/');
}
wp_safe_redirect( '/' );
exit;
}
if( isset($_GET['preview']) && $_GET['preview'] == 'false' ) {
setcookie('tgm_show_preview', '', time() - 3600, '/');
wp_safe_redirect( '/' );
exit;
}
return;
}
}
add_action('template_redirect', 'tgm_dev_preview');
// This adds CSS specific to your priview. You could do the same for JS if you needed,
// by replacing css with js, and by changing wp_enqueue_style to wp_enqueue_script. Remember to add
// dependencies for JS.
if( !function_exists( 'tgm_dev_preview_css' ) ) {
function tgm_dev_preview_css()
{
if( !$_COOKIE['tgm_show_preview'] || isset($_COOKIE['tgm_show_preview']) && $_COOKIE['tgm_show_preview'] == 'false' ) {
wp_enqueue_style('tgm-dev-css', get_stylesheet_directory_uri() . '/dev.css');
}
}
}
add_action('wp_print_styles', 'tgm_dev_preview_css', 100);
// This adds the ability to show/hide specific content from within the WP editor.
// All content between [tgm_dev_preview] content here [/tgm_dev_preview] will
// be hidden, by default. The trigger will enable it.
if( !function_exists( 'tgm_dev_preview_shortcode' ) ) {
function tgm_dev_preview_shortcode( $atts, $content = null )
{
if( isset($_COOKIE['tgm_show_preview']) && $_COOKIE['tgm_show_preview'] == 'true' ) {
$content = '';
}
return str_replace('<br />', '', $content);
}
}
add_shortcode('tgm_dev_preview', 'tgm_dev_preview_shortcode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment