Skip to content

Instantly share code, notes, and snippets.

@facelordgists
Created January 29, 2014 22:51
Show Gist options
  • Save facelordgists/8698928 to your computer and use it in GitHub Desktop.
Save facelordgists/8698928 to your computer and use it in GitHub Desktop.
PHP output buffering. Great for WordPress shortcodes
<?php
// generic usage
ob_start();
?>
<h1>Begin Content</h1>
<p>Content</p>
<?
$ob_str=ob_get_contents();
ob_end_clean();
echo $ob_str;
// Example of use in a WordPress Shortcode
add_shortcode('shortcode_name','func_shortcode_name');
function func_shortcode_name( $atts, $content = null ) {
extract( shortcode_atts( array(
'attribute1' => 'default_value',
), $atts ) );
ob_start();
?>
<h1>Shortcode Output</h1>
<p><? echo $attribute1 ?></p>
<?
$ob_str=ob_get_contents();
ob_end_clean();
return $ob_str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment