Skip to content

Instantly share code, notes, and snippets.

@RalfAlbert
Created April 12, 2013 14:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RalfAlbert/5372635 to your computer and use it in GitHub Desktop.
Save RalfAlbert/5372635 to your computer and use it in GitHub Desktop.
PHP execution shortcode for WordPress Usage: [php]<?php echo 'Hello World!'; ?>[/php]
add_shortcode(
'php',
function( $atts, $content = null ){
// un-texturize $content
$char_codes = array( '&#8216;', '&#8217;', '&#8220;', '&#8221;', '&#8242;', '&#8243;' );
$replacements = array( "'", "'", '"', '"', "'", '"' );
$content = str_replace( $char_codes, $replacements, $content );
// decode html-entities
$content = html_entity_decode( $content );
// remove whitespaces at start & end
$content = trim( $content );
// remove open tag if present
if ( true == ( '<?php' == substr( $content, 0, 5 ) ) )
$content = str_replace( '<?php', '', $content );
ob_start();
eval ($content);
return ob_get_clean();
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment