Forked from EricBusch/call-the_content-filter-once-only.php
Last active
December 2, 2017 20:54
-
-
Save EricBusch/c380916e7c185c7cb74cf596eff1ec41 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.
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
<?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