PHP Shortcode messes up formatting – a fix
-
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.
- The topic ‘PHP Shortcode messes up formatting – a fix’ is closed to new replies.