1

I am using a custom filter to place the gtag script in footer if it is applied in functions.php. The filter code is:

if (apply_filters('myplugin_ga_script_position', $position = 'footer')) {
    add_action('wp_footer', array($this, 'myplugin_add_analytics_code'));
} else {
    add_action('wp_head', array($this, 'myplugin_add_analytics_code'));
}

But when I do not use the following filter in functions.php even then the gtag script is in footer:

add_filter( 'myplugin_ga_script_position', function( $position ) {
    return 'footer';
});

Any fixes?

1
  • why is the value passed to the filter $position = 'footer' and not 'footer' or $position? It's best practice to put single statements on single lines rather than combining them like that as it leads to mistakes and cryptic errors. It also doesn't make sense that you're running apply_filters in an if statement which suggests it will return true or false, but passing it strings of text instead. Should there be a === 'footer' in the conditional?
    – Tom J Nowell
    Mar 25 at 17:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.