Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
This is an example for ensuring your content filter function is called once and only once during the page load of a single Post|Page|Product|etc... This is useful when you need to modify the $content via the 'the_content' filter but you don't want to perform the process more than once because it's server and/or time intensive.
<?php
/**
* Example of how to execute the "the_content" filter ONLY once.
*
* @param string $content
*
* @return string $content
*/
function mycode_the_content( $content ) {
if ( ! in_the_loop() ) {
return $content;
}
if ( ! is_singular() ) {
return $content;
}
if ( ! is_main_query() ) {
return $content;
}
$content = ucwords( $content );
remove_filter( current_filter(), __FUNCTION__ );
return $content;
}
add_filter( 'the_content', 'mycode_the_content' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment