WordPress does automatic paragraph formatting using the wpautop filter, some PHP code originally developed by Matt Mullenweg. For most of the time that this blog has existed, I’ve disabled the wpautop filter using the following two lines in my theme’s functions.php file:
remove_filter('the_content', 'wpautop');
remove_filter('the_excerpt', 'wpautop');
I’m not the only one to do this. But, I’m tired of having to manually type <p>s for every paragraph in every post that I write.
I’d like to be able have it back on, but preferably a bit smarter so that you don’t get all that crap (also common on bulletin boards with empty paragraphs around undetected block-level elements, especially if these are non-standard, related to plugins.
At the very least I want to be able to turn it off for when it does annoy me, on these moments that I don’t want auto-anything. Also, I need this if I don’t want to break the nearly 300 posts that I formatted manually. So, I want to use a custom field to turn the filter on or off.
I found an unpublished plugin (unpublished in the sense that the source isn’t hosted somewhere proper such as wordpress.org/extend/plugins/) which does some of what I want in a somewhat messy unmaintained manner. After years of just entering those damn <p>s (and over a year since this draft was in the making), I decided to do my own plugin for post-by-post (and global) control of the wpautop filter. It’s called wpautop-control:
<?php
/*
Plugin Name: wpautop-control
Plugin URI: http://blog.bigsmoke.us/tag/wpautop-control/
Description: This plugin allows you fine control of when and when not to enable the wpautop filter on posts.
Author: Rowan Rodrik van der Molen
Author URI: http://blog.bigsmoke.us/
Version: 1.0
*/
if ( is_admin() ) {
add_action('admin_menu', 'wpautop_control_menu');
add_action('admin_init', 'wpautop_control_settings');
function wpautop_control_menu() {
add_submenu_page('options-general.php', 'wpautop-control', 'wpautop control', 'manage_options', 'wpautop-control-menu', 'wpautop_control_options');
}
function wpautop_control_options() {
if (!current_user_can('manage_options')) {
wp_die( __('You do not have sufficient permissions to access this page.') );
}
?>
<div class="wrap">
<h2>wpautop control options</h2>
<form method="post" action="options.php">
<?php settings_fields('wpautop-control') ?>
<table class="form-table">
<tr valign="top">
<th scope="row">wpautop filter on by default?</th>
<td>
<label><input type="radio" name="wpautop_on_by_default" value="1" <?php if ( get_option('wpautop_on_by_default') == '1' ) echo 'checked="1"' ?>> yes</label>
<label><input type="radio" name="wpautop_on_by_default" value="0" <?php if ( get_option('wpautop_on_by_default') == '0' ) echo 'checked="1"' ?>> no</label>
</td>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="Save Changes" />
</p>
</form>
</div>
<?php
}
function wpautop_control_settings() {
register_setting('wpautop-control', 'wpautop_on_by_default', 'intval');
}
}
else { // ! is_admin()
add_filter('the_content', 'wpautop_control_filter', 9);
function wpautop_control_filter($content) {
global $post;
// Get the keys and values of the custom fields:
$post_wpautop_value = get_post_meta($post->ID, 'wpautop', true);
$default_wpautop_value = get_option('wpautop_on_by_default');
$remove_filter = false;
if ( empty($post_wpautop_value) )
$remove_filter = ! $default_wpautop_value;
elseif ($post_wpautop_value == 'true')
$remove_filter = false;
elseif ($post_wpautop_value == 'false')
$remove_filter = true;
if ( $remove_filter ) {
remove_filter('the_content', 'wpautop');
remove_filter('the_excerpt', 'wpautop');
}
return $content;
}
}
?>
(I’ve requested the plugin to be added to the WordPress plugin repository, so that it won’t have to be reinvented another 20 times in the next year or so.) 😉
After installing the plugin, you can choose whether to enable or disable wpautop by default. Then, for every post where you want to deviate from the default, you can set the wpautop custom field to ‘true’ or ‘false’.
I want the new default to be to enable the filter, but since all my old posts have been manually formatted, I want all these to have the wpautop field added and set to ‘false’.
Adding the appropriate custom field values to all existing posts is easy thanks to MySQL’s INSERT … SELECT syntax:
INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT wp_posts.ID, 'wpautop', 'false'
FROM wp_posts
WHERE post_type = 'post';
Recent Comments