Site icon Hip-Hop Website Design and Development

Call to undefined function is_home() or any conditional tags

Currently I am coding in my own custom plugin. For some reason I can’t use conditional tags inside my function. For example

 function check_home() {
    if(is_home()) {
       $my_val=1;
    } else {
     $my_val=2;
    }
    return $my_val;
  }

It’s returning following error

Fatal error: Uncaught Error: Call to undefined function is_home()

I checked and I found a solution & it is working fine.

It saying to add this add_action('wp', 'check_home');

But now I want to get the value of function to variable. But it is showing error.

 add_action('wp', 'check_home');
 function check_home() {
    if(is_home()) {
       $my_val=1;
    } else {
     $my_val=2;
    }
    return $my_val;
  }
  
  $my_ver = check_home();
  echo $my_ver;

Then it showing following error.

Fatal error: Uncaught Error: Call to undefined function is_home()

Now how can i solve this error ? What i am missing ?