• Resolved kaiserthegreat

    (@kaiserthegreat)


    This plugin is cool except that it’s all or nothing, and I want the option to turn line breaks on or off depending on the complexity of my post.

    This article has more promise, but requires tampering with core code, and I would rather it be a plugin.

    So…trying to combine the best of both worlds (and write my first plugin), I have:

    function disable_linebreaks($content) {
            if (preg_match('/<!--DISABLE_LINEBREAKS-->/', $content)) {
                  remove_filter ('the_content','wpautop');
            }
    }
    
    add_action('save_post','disable_linebreaks');

    Not working of course, as I’m here, but it’s possible right? Can a plugin guru correct my errors?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    The autop stuff doesn’t happen when the post is saved, it happens when the post is displayed.

    So disabling autop when you save the post is useless. You need to disable or enable it when you are in the Loop showing the post.

    Also, preg_match is a bad idea when you’re just checking for the existence of a string. strpos is way faster.

    function disable_linebreaks($content) {
            if (strpos($content,'<!--DISABLE_LINEBREAKS-->') !== false) {
                  remove_filter ('the_content','wpautop');
            } else {
                  // remove then add to ensure there's only one
                  remove_filter ('the_content','wpautop');
                  add_filter ('the_content','wpautop');
            }
            return $content;
    }
    // add to the content with a high priority
    add_filter('the_content','disable_linebreaks',1);

    That may or may not work, I dunno. Off the cuff, have not tested it.

    Thread Starter kaiserthegreat

    (@kaiserthegreat)

    It didn’t work as-is, but changing the strpos function back to preg_match made it work, so strpos must be returning false for some reason. But you’re right, preg_match definitely slows load time a bit. Will do further research but THANKS for getting it to work for me, one way or the other!

    kaiserthegreat – Did you get this worked out?

    rolandog

    (@rolandog)

    Hey guys, I wrote this plugin yesterday and until now I stumbled through this support post. I managed to make it work for pages and single posts.

    <?php
    /*
    Plugin Name: rmWPautop
    Plugin URI: https://rolandog.com/archives/2008/11/12/wp-plugin-rm-wpautop/
    Description: This plugin allows you to set a 'rm_autop' custom field in your pages or posts and allow for WordPress not to add automatically paragraph elements.
    Author: Rolando Garza
    Author URI: https://rolandog.com/
    Version: 0.2
    
    */ 
    
    function rm_autop() {
    	global $posts;
    
        // get the posts
        foreach ($posts as $post) {
    	    // Get the keys and values of the custom fields:
    	    $id = $post->ID;
    	    $rmautop = get_post_meta($id, 'rm_autop', true);
    
    	    // Remove the filter
    	    if ($rmautop === 'true') {
                remove_filter('the_content',  'wpautop');
    	    }
        }
    }
    
    // Hook into the Plugin API
    add_action('wp_head', 'rm_autop');
    
    ?>

    Rather than write something into your post, you specify a Custom Field with a key of ‘rm_autop’ and a value of true (without the quotes).

    Your post/page will show without autop’s breaks and paragraph elements. You also have to take into account that plugins like Snipplr’s will compensate for the implied autop filter. I wasn’t validating at first and I kept thinking I was doing something wrong.

    rolandog

    (@rolandog)

    Sorry, I had a small bug, fixed it now. It seem it was best to call this to the_content, and I also changed the key to rm_wpautop.

    <?php
    /*
    Plugin Name: rm_wpautop
    Plugin URI: https://rolandog.com/archives/2008/11/12/wp-plugin-rm-wpautop/
    Description: This plugin allows you to set a 'rm_wpautop' custom field in your pages or posts and allow for WordPress not to add automatically paragraph elements.
    Author: Rolando Garza
    Author URI: https://rolandog.com/
    Version: 0.2
    */
    
    function rm_wpautop() {
        global $posts;
        // get the posts
        foreach ($posts as $post) {
            // Get the keys and values of the custom fields:
            $id = $post->ID;
            $rmwpautop = get_post_meta($id, 'rm_wpautop', false);
    
            // Remove the filter
            if (count($rmwpautop)) {
                remove_filter('the_content', 'wpautop');
            } else {
                remove_filter('the_content', 'wpautop');
                add_filter('the_content', 'wpautop');
            }
        }
    }
    
    // Hook into the Plugin API
    add_action('the_content', 'rm_wpautop');
    
    ?>
    rolandog

    (@rolandog)

    Ok, third time’s the charm. The other versions didn’t output valid XHTML. I reviewed the code and it seems it was all about adding the filter with a different priority.

    <?php
    /*
    Plugin Name: rm_wpautop
    Plugin URI: https://rolandog.com/archives/2008/11/12/wp-plugin-rm-wpautop/
    Description: This plugin allows you to set a 'rm_wpautop' custom field in your pages or posts and allow for WordPress not to add automatically paragraph elements.
    Author: Rolando Garza
    Author URI: https://rolandog.com/
    Version: 0.3
    */
    function rm_wpautop($content) {
        global $post;
        // Get the keys and values of the custom fields:
        $rmwpautop = get_post_meta($post->ID, 'wpautop', true);
        // Remove the filter
        remove_filter('the_content', 'wpautop');
        if ('false' === $rmwpautop) {
        } else {
            add_filter('the_content', 'wpautop');
        }
        return $content;
    }
    // Hook into the Plugin API
    add_filter('the_content', 'rm_wpautop', 9);
    ?>
    Dgold

    (@dgold)

    Thank you for making a new plugin.

    rolandog

    (@rolandog)

    No problem. =) But, a small note: you have to set a custom field of wpautop to false so that the plugin works. I changed it so that it seemed more logical.

    Very nicely done.

    I have spent hours trying to solve this WP problem – and this did it! Thank you so much for taking the time to create it.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Removing wpautop Filter’ is closed to new replies.