Skip to content

Instantly share code, notes, and snippets.

@EricBusch
Last active November 6, 2022 11:06
Show Gist options
  • Save EricBusch/500b2eba17b6d39ad09e225889754228 to your computer and use it in GitHub Desktop.
Save EricBusch/500b2eba17b6d39ad09e225889754228 to your computer and use it in GitHub Desktop.
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
/**
* 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.
*/
add_filter( 'the_content', 'mycode_the_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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment