Last active
September 21, 2017 21:57
-
-
Save kingkool68/0e79d7ff219c6270a5682bb6b9059300 to your computer and use it in GitHub Desktop.
Using `wp_debug_backtrace_summary()` to only filter something when called from a particular function or method
This file contains hidden or 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
/* | |
Here we're filtering the site_url() and get_site_url() functions which are called dozens and dozens of times during a request. | |
In this case we only want to modify $url if the filter was called from a particular PHP class (rtCamp\WP\Nginx\Helper) | |
*/ | |
add_filter( 'site_url', function( $url = '' ) { | |
$backtrace = wp_debug_backtrace_summary(); | |
if ( stripos( $backtrace, 'rtCamp\WP\Nginx\Helper' ) ) { | |
$url = str_replace( 'https://', 'http://', $url ); | |
} | |
return $url; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment