Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This may have a simple answer but I would like to store all the variables I've declared ( or cached ) across my theme in a central file and serve then this file globally ( Similar to how get_header(), get_footer() and get_sidebar() works ).

I've tried creating a file called variables.php and serving it via get_template_part but without success.

For this particular project, it would be more convenient if the multitude of variables I've created across my theme were housed in a central location in lieu of being scattered throughout various theme files.

Is there a idiomatic way of accomplishing this in WordPress?

share|improve this question
1  
I've removed the PHP tag as well as my question (not important) from the comments. Still: How is this specific to WordPress and not just PHP in context of WP? –  kaiser Feb 17 at 19:56
    
@kaiser The question is regarding a WordPress theme. I suppose a pure PHP solution could exist and would work but the preferred answer would be an idiomatic WordPress solution. –  Wilhelm Feb 17 at 20:07
    
Just use require_once/include_once in your functions.php if it's a theme issue. –  Andrew Bartel Feb 17 at 20:32
    
have to agree with kaiser, how you structure your code is not related to wordpress. In general of course you should have exactly zero global variable which will solve your problem, wordpress has already too many globals as it is. –  Mark Kaplun Feb 17 at 21:36
1  
You seem to describe configuration file rather than "variables" one. WordPress has no configuration file concept or convention (wp-config.php being just a place where constant definitions are dumped) so it's up to you (and generic PHP) how to implement it. –  Rarst Feb 17 at 22:51
show 5 more comments

1 Answer 1

up vote 0 down vote accepted

A 'better' way of storing variables for global use is to use add_option() to cache variables and get_option() for retrieving variables. This way everything stored in the database and you can access it anywhere.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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