Created
November 14, 2014 01:05
-
-
Save ibrokemywp/f8fb5954cca70b76bf4a to your computer and use it in GitHub Desktop.
Applying the_content filter inside the_content filter causes lots of tears
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* I'm going to add some content from one post to | |
* the content of another post. Yay for me. | |
*/ | |
add_filter( 'the_content', 'my_little_content_addition' ); | |
function my_little_content_addition( $content ) { | |
if ( is_the_right_content() ) { | |
/** | |
* Let's go get another post's content to add | |
* to this post | |
*/ | |
$post = get_post( $my_post_id ); | |
/** | |
* I want the post to have all the proper HTML | |
* markup, so let's run the_content filter on | |
* it. | |
*/ | |
$content .= apply_filters( 'the_content', $post->the_content ); | |
/** | |
* You've now made everyone cry by calling | |
* the_content filter form within the_content | |
* filter. In my case, it caused all sorts of | |
* implosions with page builders and WPML. | |
* | |
* If you want to add content formatting | |
* run it through wpautop() instead. | |
* | |
* $content .= wpautop( $post->the_content ); | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment