0

I create a new wordpress without theme and with only one plugin: GDPR WP. (I want to try this plugin on empty wordpress before deploy it on other website).

So, on a first time i try to create function to accept or not GoogleAnalytics cookie. My function work fine but i have on error on the BO/FO:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'cookie_GA' not found or invalid function name in C:\wamp64\www\wordpress\wp-includes\class-wp-hook.php on line 286

This is my function:

add_action( 'init', 'cookie_GA' );  

if (is_allowed_cookie('_ga')) {
    function cookie_GA() { ?>

        <script async src="https://www.googletagmanager.com/gtag/js?id=UA-91484621-2"></script>
        <script>
          window.dataLayer = window.dataLayer || [];
          function gtag(){dataLayer.push(arguments);}
          gtag('js', new Date());

          gtag('config', 'UA-91484621-2');
        </script>

        <?php 
    };
};

Thanks for your help.

2

The warning is clear:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'cookie_GA' not found or invalid function name in C:\wamp64\www\wordpress\wp-includes\class-wp-hook.php on line 286

So cookie_GA() is not found. When can this be the case? Well, of course when is_allowed_cookie('_ga') is false. So either wrap the add_action() call inside that if as well or create a dummy function.

if (is_allowed_cookie('_ga')) {
    add_action( 'init', 'cookie_GA' );
    function cookie_GA() {  /* your code */ }
}
  • Beginner's mistake, thanks for your help. Its work – TiboK May 29 '18 at 11:32
  • @TiboK You're welcome. If this was a typo-kinda mistake, feel free to delete the question altogether. If you think future visitors might find this usefil, please consider marking my answer as accepted – kero May 29 '18 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.