0

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?

9
  • 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, 2014 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, 2014 at 20:07
  • Just use require_once/include_once in your functions.php if it's a theme issue. Feb 17, 2014 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. Feb 17, 2014 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, 2014 at 22:51

1 Answer 1

0

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.

Your Answer

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

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