Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.