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.
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?
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: http://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: http://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.
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: http://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: http://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');
?>
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: http://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: http://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);
?>
Thank you for making a new plugin.
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.
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.