I'm a web developer who works on WordPress sites and want to ensure I keep PHP variables out of the global scope. I have questions about this:

  1. If I created a $new_title = "test seo title"; variable in an active theme's functions.php file, is that $new_title variable accessible (not undefined) in the active theme's PHP files? Is that variable also accessible in the active plugin files?

  2. Also, if I created a $new_title2 = "test seo title2"; variable inside an active plugin, is that $new_title2 variable accessible (not undefined) in active theme's functions.php file and other theme PHP files?

2 Answers 2


Nope! There is no way. variable scope is file based in PHP, It means you can't access a variable or even function and class from another file unless you include that file inside current file or use other similar ways like namespace or API call to get desired value from the file.

you can read more about variable scope in PHP: https://www.php.net/manual/en/language.variables.scope.php

  • 1
    Thanks mahdi azarm. Yes, I'm trying to do WordPress development without introducing global variables anyway so that's good news! Apr 9, 2022 at 21:25
  • @risingPhoenix1979 I'm happy to help! Try to read more and use them because they are so handy in anyway Apr 12, 2022 at 1:58

Your variables inside of your PHP file is not accessible inside of other PHP files unless you declare it as a global variable. You will also have to call it as a global variable again.

global $new_title;
$new_title = "test seo title2";

then on the other file you will:

global $new_title;
echo $new_title;//should display "test seo title2";
  • Thanks @stunt_crazy. However, I want to avoid adding custom global PHP variables in my WordPress theme or plugins. Yet, I'm find with predefined WordPress global variables, like: global $post; Apr 22, 2022 at 0:06

Your Answer

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

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