• Resolved Claude Vedovini

    (@cvedovini)


    Hi,

    I just installed and started to use your plugin but I quickly discovered it was messing with the formatting on some pages even tho I don’t use the shortcode on those.

    So I took look at the code and saw this:

    remove_filter( 'the_content', 'wpautop' );
    add_filter('the_content', 'wpautop' , 99);
    add_filter('the_content', 'shortcode_unautop',100 );

    So that means a lot of things that didn’t go through wpautop before are now being reformatted. That includes every shortcodes.

    There’s a much better solution that I found here: http://bit.ly/1LM05R1 and that inspired me to find a much better solution. Here’s the modified markdown_shortcode.php that I am using:

    include('parsedown/Parsedown.php');
    include('parsedown/ParsedownExtra.php');
    
    $markdown_shortcode_content = array();
    
    function markdown_shortcode($attr, $content = null) {
        global $markdown_shortcode_content;
        $content = $markdown_shortcode_content[$content];
    
        $content = undo_html_entities($content);
        $content = trim($content);
        $content = underscores_to_spaces($content);
    
        $extra = new ParsedownExtra();
        $parsed_content = $extra->text($content);
    
        return $parsed_content;
    }
    add_shortcode('markdown', 'markdown_shortcode');
    
    function markdown_shortcode_pre($attr, $content = null) {
        global $markdown_shortcode_content;
        $key = md5($content);
        $markdown_shortcode_content[$key] = $content;
        return "[markdown]{$key}[/markdown]";
    }
    
    function markdown_shortcode_preprocess($content) {
        global $shortcode_tags;
    
        // Backup current registered shortcodes and clear them all out
        $orig_shortcode_tags = $shortcode_tags;
        $shortcode_tags = array();
    
        add_shortcode('markdown', 'markdown_shortcode_pre');
    
        // Do the shortcode (only the one above is registered)
        $content = do_shortcode($content);
    
        // Put the original shortcodes back
        $shortcode_tags = $orig_shortcode_tags;
        return $content;
    }
    add_filter('the_content', 'markdown_shortcode_preprocess', 1);
    
    // Reverts changes that were applied by the Visual editor
    function undo_html_entities($content){
        $content = str_replace("<", "<", $content);
        $content = str_replace(">", ">", $content);
        $content = str_replace("&", "&", $content);
        return $content;
    }
    
    // Replaces more than one underscore to same amount of spaces
    function underscores_to_spaces($content) {
        $content = preg_replace_callback('/_{2,}/', function ($matches) {
            return str_replace('_', ' ', $matches[0]);
        }, $content);
        return $content;
    }
    
    function init_highlight() {
        wp_enqueue_style("highlight",  plugin_dir_url(__FILE__) . 'highlight/styles/github.css');
        wp_enqueue_script("highlight",  plugin_dir_url(__FILE__) . 'highlight/highlight.min.js');
        wp_enqueue_script("highlight_init",  plugin_dir_url(__FILE__) . 'init_highlight.js');
    }
    add_action('init', 'init_highlight');

    Also, I don’t know why you added that last part, the code doesn’t tell but I think you should at least document that side effect or just don’t do it if you can avoid it (I removed it).

    // Stop WordPress converting quotes to pretty quotes (nobody will miss them)
    remove_filter('the_content', 'wptexturize');

    also, you should prefix all your methods with a unique prefix or use an object because you’re going to create conflicts.

    Hope that helps

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘unwelcome side effects’ is closed to new replies.