0

I have $variable_1 = '5'; in /public/includes/themes/theme-section-users-score.php and I want to use its value in another php file with a different location /plugins/name/other/users-time.php , I tried using include_once , require_once and others but I can't, might be the .htaccess doesn't allow me? please help, thanks in advance.

3
  • Is the value hardcoded? What if you want to change the value? If it's hard coded, then you could very easily just write the value out in both the theme and the plugin, but if it's not going to be hard coded then that means you'll be writing it to the database, so instead of pulling the variable across why not just check the database when you need it? Commented Dec 31, 2020 at 19:40
  • the value is variable its not hard coded, tell me how its easier, from DB or from another file
    – Jimmy
    Commented Jan 2, 2021 at 11:28
  • If you're obtaining the variable value in one file use the same method to obtain that variable in the other file, that way neither is dependent on the other. How do you get the variable in the theme file? Commented Jan 3, 2021 at 18:06

1 Answer 1

1

Store the value is a function or make it global;

function get_variable_1(){

    return 5;

}

OR

global $variable_1;
$variable_1 = 5;

But.. really, what you need to do it to read a bit more about the basic of PHP to understand how variables and functions work - global variables are not really a good idea and neither are functions which single return values with accepting arguments.. so you need to plan your PHP application to use both concepts correctly.

2
  • with global variable i cannot access it from a plugin file to a theme php file...
    – Jimmy
    Commented Jan 3, 2021 at 1:51
  • You need to declare new variables as global - and then you also need to use the global keywork to bring the variable into the scope of your function again - but, global variables are not a great solution.. WordPress uses them a lot, but -- oddly -- that's not because it's the best way to do things..
    – Q Studio
    Commented Jan 3, 2021 at 8:56

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.