Skip to content

Instantly share code, notes, and snippets.

@kingkool68
Last active September 21, 2017 21:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kingkool68/0e79d7ff219c6270a5682bb6b9059300 to your computer and use it in GitHub Desktop.
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
/*
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