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?
$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 runningapply_filters
in anif
statement which suggests it will returntrue
orfalse
, but passing it strings of text instead. Should there be a=== 'footer'
in the conditional?