Skip to content

Instantly share code, notes, and snippets.

@robertuniqid
Last active August 12, 2018 14:58
Show Gist options
  • Save robertuniqid/38ef4a4836f5fa1b8d3137f862759df0 to your computer and use it in GitHub Desktop.
Save robertuniqid/38ef4a4836f5fa1b8d3137f862759df0 to your computer and use it in GitHub Desktop.
Example on how the Gutenberg the_content filter for wpautop could've prevented breaking plugins that removed wpautop.
<?php
// How it is :
function gutenberg_wpautop( $content ) {
if ( gutenberg_content_has_blocks( $content ) ) {
return $content;
}
return wpautop( $content );
}
remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'gutenberg_wpautop', 8 );
// How I would've done it :
class GTCreativeExampleWPAutop {
public $removed_filter = false;
public function __construct() {
add_filter( 'the_content', [ $this, '_early_the_content' ], 8 );
add_filter( 'the_content', [ $this, '_late_the_content' ], 10000 );
}
public function _early_the_content( $content ) {
if( has_filter( 'the_content', 'wpautop' ) ) {
$this->removed_filter = true;
remove_filter( 'the_content', 'wpautop' );
} else {
$this->removed_filter = false;
}
if ( gutenberg_content_has_blocks( $content ) ) {
return $content;
}
if( $this->removed_filter )
return wpautop( $content );
else
return $content;
}
public function _late_the_content( $content ) {
if( $this->removed_filter )
add_filter( 'the_content', 'wpautop' );
return $content;
}
}
$go = new GTCreativeExampleWPAutop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment