Where to place a global function?

Where’s or how is the best way to include a global function? I have done a small cookie consent for me and now I have to check at multiple places for the cookie with this:

if ( Cookie::get('consent') == 'all' && $page->uid() != 'error' ) {
  // stuff here

A plugin file… Either as a standard function, or as a page method (or whatever type of method would apply here).

That was also my try, I thought maybe there’s a better way or best practice :slight_smile:

Another short question which is more PHP related. If I create a function like this:

function checkConsent ( $code ) {
  if ( Cookie::get('consent') == 'all' && $page->uid() != 'error' ) {
    echo $code;

I call it like this checkConsent('my-code-here');

But I have noticed that some of the analytics codes have a lot of ’ and ", is there any way to avoid this or works only with escaping? Cause this will make problems:


No, passing the strings itself as a parameter to the function doesn’t make sense.

You have at least two options:

  1. Store the strings in a variable that you pass to the function.
  2. Don’t pass them as parameter but just echo the string(s) from the function

All right. I only thought there’s a hidden “trick” or similar. Thanks so much!

That was the trick. In fact, it would look really dirty to use a long string as function argument.

In fact, if you have different analytics strings, you would probably want to store them somewhere (in the site content or config or a text file) and then retrieve from there to pass as argument.

Exact that was my thought also! I have made me a textarea in site config and directly outputs the value of this field into the function argument :slight_smile: