Skip to content

Instantly share code, notes, and snippets.

@SiGaCode
Last active June 16, 2017 14:50
Show Gist options
  • Save SiGaCode/f5c103faee3df0acc5486ac458d2525d to your computer and use it in GitHub Desktop.
Save SiGaCode/f5c103faee3df0acc5486ac458d2525d to your computer and use it in GitHub Desktop.
"Here’s a plugin I’ve come up with to disable wpautop on specific posts/pages." From: https://grahamwalters.me/lab/disable-wpautop-on-specific-postspages/ You can place the above code in a file called disable_wpautop.php and upload it to your plugins folder. How do you use it? Easy, you simply add a custom field: Name: wpautop Value: false Secon…
<?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');
?>
<?php
// https://gist.github.com/ninnypants/1668216
// Removed br from the code
// Goes to a hookbox in combination with a lable, so you can apply it to certain pages
add_filter( 'the_content', 'remove_empty_p', 20, 1 );
function remove_empty_p( $content ){
// clean up p tags around block elements
$content = preg_replace( array(
'#<p>\s*<(div|aside|section|article|header|footer)#',
'#</(div|aside|section|article|header|footer)>\s*</p>#',
'#</(div|aside|section|article|header|footer)>\s*<br ?/?>#',
'#<(div|aside|section|article|header|footer)(.*?)>\s*</p>#',
'#<p>\s*</(div|aside|section|article|header|footer)#',
), array(
'<$1',
'</$1>',
'</$1>',
'<$1$2>',
'</$1',
), $content );
return preg_replace('#<p>(\s|&nbsp;)*(\s|&nbsp;)*</p>#i', '', $content);
}
?>
<?php
/**
* Remove empty paragraphs created by wpautop()
* CAUTION: Can break form plugins!
* @author Ryan Hamilton
* @link https://gist.github.com/Fantikerz/5557617
* @article https://gomakethings.com/remove-empty-paragraphs-added-by-wordpress/
*/
function remove_empty_p( $content ) {
$content = force_balance_tags( $content );
$content = preg_replace( '#<p>\s*+(<br\s*/*>)?\s*</p>#i', '', $content );
$content = preg_replace( '~\s?<p>(\s|&nbsp;)+</p>\s?~', '', $content );
return $content;
}
add_filter('the_content', 'remove_empty_p', 20, 1);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment