Created
April 12, 2013 14:57
-
-
Save RalfAlbert/5372635 to your computer and use it in GitHub Desktop.
PHP execution shortcode for WordPress
Usage:
[php]<?php echo 'Hello World!'; ?>[/php]
This file contains 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
add_shortcode( | |
'php', | |
function( $atts, $content = null ){ | |
// un-texturize $content | |
$char_codes = array( '‘', '’', '“', '”', '′', '″' ); | |
$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