Despite being creaky with age, this plugin used to do what it says on the tin. Easy, straightforward, etc. However, since PHP7, I believe it’s time to retire this plugin.
]]>I was looking for a simple way to add PHP to posts, etc. This plugin is no good because once it is activated it messes up CSS on my site.
]]>I found that if I display a post at the top of a page, for instance either using the Get Latest Post plugin or coding it by hand, then if the PHP shortcode plugin is enabled, the remainder of the page is incorrectly formatted and so loses its paragraph breaks.
This is because of the re-ordering of the shortcode handling. The plugin removes the do_shortcode handling from priority 11 and puts it in at priority 9, so that the PHP shortcode gets processed before other formatting.
This can be fixed by adding a function which processes just the PHP shortcodes without any other shortcodes.
The fix can be added either in the plugin’s php-shortcode.php file or in your theme’s functions.php. (Adding it in your functions.php means that it will not be removed if the plugin is updated or removed. The choice is yours.)
Whichever you choose, add the following function to the file:
// fix for PHP shortcode
function do_php_shortcode_fix($content) {
global $shortcode_tags;
// save current shortcodes
$old_shortcode_tags = $shortcode_tags;
// remove all shortcodes, then re-add just our php and echo shortcodes
remove_all_shortcodes();
add_shortcode('php', $old_shortcode_tags['php']);
add_shortcode('echo', $old_shortcode_tags['echo']);
$content = do_shortcode($content);
// and now put back the original shortcodes
$shortcode_tags = $old_shortcode_tags;
return $content;
}
If you are making the fix in the php-shortcode.php file, then you need to comment out the lines in the php_shortcode_init function that move the filter:
//if(remove_filter('the_content','do_shortcode', 11))
// add_filter('the_content','do_shortcode',9);
Then, below those lines, add the following:
// filter the content to deal with just the php and echo shortcodes
// early on as priority 9 (before any WP formatting)
add_filter('the_content', 'do_php_shortcode_fix', 9);
Alternatively, if you are adding the fix to your functions.php, or you want to avoid changing the existing function, then you can add the following code:
function fix_php_shortcode_init() {
global $shortcode_tags;
if (isset($shortcode_tags['php'])) {
// Move do_shortcode back to default of priority 11
if(remove_filter('the_content','do_shortcode', 9))
add_filter('the_content','do_shortcode', 11);
// filter the content to deal with just the php and echo shortcodes
// early on as priority 8 (before any WP formatting)
add_filter('the_content', 'do_php_shortcode_fix', 8);
}
}
// add fix for PHP shortcode, with priority 12 so it
// comes after the PHP shortcode has been initialised
add_action('init','fix_php_shortcode_init', 12);
You should now be able to use the plugin without it breaking the formatting elsewhere.
]]>PHP Shortcode seriously breaks the content passed to other spanning shortcodes (e.g., [some_code]content[/some_code]. The content is almost, but not quite a valid string. String functions like strpos for substr fail to return correct results on the string processed by PHP Shortcode. This is true even if you don’t have any code within the content that use PHP Shortcode.
]]>Widgets with the [php] hook fail to display and stops loading the HTML
]]>I have been using this successfully previously, but now it doesn’t work – the code just shows up instead of executing.
Does this not work with 3.0? Any update?
]]>