Rather than add your filter immediately when the functions.php
file loads, find actions that you can hook onto later, when you know the context of the current request, so you can add and remove filters in a targeted way.
For example, you might be able to use loop_start
to only affect WPBakery output in the main content area:
add_action( 'loop_start', 'wpd_loop_start_example' );
function wpd_loop_start_example( $query ){
if( $query->is_main_query() && is_home() ){
// The loop for the main query is starting, and it's the home page
add_filter( 'author_link', 'change_author_link', 10, 1 );
}
}
You can also remove filters, so later content won’t be altered:
add_action( 'loop_end', 'wpd_loop_end_example' );
function wpd_loop_end_example( $query ){
if( $query->is_main_query() && is_home() ){
remove_filter( 'author_link', 'change_author_link', 10 );
}
}
template_redirect
or wp
actions might be other candidates for adding filters after the main query is set up but before output starts.