Graham Walters

Disable wpautop on specific posts/pages!

Every now and then you want to build a html page, and not have wordpress destroy it. Basically every time a post/page is requested from wordpress, it executes a bunch of filters which format your post/page by wrapping each paragraph in <p> </p> tags, but sometimes you don’t want it to.

Here’s a plugin I’ve come up with to disable wpautop on specific posts/pages.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
/*
 * Plugin Name: Disable wpautop
 * Plugin URI: https://grahamwalters.me/2014/03/07/disable-wpautop-on-specific-postspages/
 * Author: Graham Walters
 * Author URI: https://grahamwalters.me
 * Version: 1.1
 * Description: Disable wpautop on posts/pages with custom field 'wpautop' == false.
 */

function custom_wpautop($content) {
  if (get_post_meta(get_the_ID(), 'wpautop', true) == 'false')
    return $content;
  else
    return wpautop($content);
}

remove_filter('the_content', 'wpautop');
add_filter('the_content', 'custom_wpautop');
?>

How does it work?

Simple, the plugin starts by replacing the wpautop filter function with custom_wpautop. Then the function gets the current post/page ID and checks if there is a custom field called wpautop set to false. If the custom field exists, the content is returned without running the wpautop() function. If the field doesn’t exist, the content is returned after running the wpautop() function.

How do you use it?

Easy, you simply add a custom field:

Name: wpautop
Value: false

Anyone is free to use the above plugin, but please leave the header intact. You can place the above code in a file called disable_wpautop.php and upload it to your plugins folder:

/wp-content/plugins/

This entry was tagged wpautop