I am not sure how relevant this is,i am working with Java based web-application, so in some cases we need to define some properties which can be used in entire web application like

  1. Google API Key
  2. Any other such key value

We tend to define a property file with extension .properties and than JAVA API provide a way to read this file, all we need to pass key name to the API and it wil return the value.

While working with Wordpress API, i need to have such use case where i want to define such properties so that i can read them where ever i need those values, i am aware that WordPress read data from its config file but not sure how.

This is what i am trying

  1. Define a file in my theme folder say customProperties
  2. Define some key value pairs inside the file

and than where i need them, all i need to pass the key and it should return me the value associated with it.

I am sure this can be done with the help of DB, but i am looking for file based approach. One option which seems relevant to me is to define those in my theme's function.php file

any pointer for this?

2 Answers 2


You certainly could define those values as PHP constants and/or global variables in, for instance, functions.php.

I understand you would, at this point, rather go with a "file based approach" than using the database, but nonetheless, I have to mention that that would be both the standard as well as the best way of doing things.

Have a look at the WordPress Options API.

I haven't quite understood whether you're developing a plugin or a theme (you've tagged the question , but in the question itself you speak of your theme's files).
At any rate, options would be the way to go.

  • Thanks for the inputs and they have cleared my doubts,well i am not that much proficient to go for themes ;)working on customize existing WordPress theme Mar 24, 2013 at 10:43

i am aware that WordPress read data from its config file but not sure how

No, WordPress does not 'read' the configuration, WP includes it. WordPress defines a lot of global constants (e.g. WP_DEBUG ). This global constants are accessible all over the script. You can do it in the same way:

$key_values = array(
  'key_one' => 'value_one',
  'key_two' => 'value_two',
  'key_3'   => 3,

foreach ( $key_value as $key => $value ) {
  if ( ! defined( $key ) )
    define( $key, $value );

And in your functions.php include it with require_once 'my_propperties.php';

But this is not the preferred way. It is risky because you have to be sure none of your keys match another existing constant. And with this solution, you will poisoned the global namespace.

Another solution is to use a single object.

class My_Container_For_Data
  public $key_one = 'value_one';
  public $key_two = 'value_two';

  const KEY_THREE = 3;


global $my_data_container;
$my_data_container = new My_Container_For_Data;

And access the values with

global $my_data_container;
$key_one = $my_data_container->key_one;
$const_three = $my_data_container::KEY_THREE;

Now you just have be sure your classname does not match an existing classname.

Maybe you don't want to create a 'static' data container and want to read the key-value pairs from a file. No problem at all:

 * content of file data_csv.propperties:
 * key_one,value_one
 * key_two,value_two
 * key_3,3
class My_Data_Container
    public static $propperties_file = 'data_csv.propperties';

    public static $data = array();

    public function __construct() {

        // maybe add some path info to $propperties_file

        if ( empty( self::$data ) )


    public function __get( $key ) {

        return ( isset( self::$data[ $key ] ) ) ?
            self::$data[ $key ] : false; // or null, or trigger error, or ...


    public function __set( $key = '', $value = null ) {

        if ( empty( $key ) )
            return false;
            $key = (string) $key;

        self::$data[ $key ] = $value;


    protected function read_propperties() {

        $handle = fopen( self::$propperties_file, 'r' );

        if ( false != $handle ) {

            while ( $data = fgetcsv( $handle, 1024, ',' ) ) {
                $this->$data[0] = $data[1];

        } else {
            trigger_error( 'Could not open propperties file', E_USER_ERROR );



Here I use a file with simple comma seperated values. Other formats like XML are maybe better, but you have to add a function to parse the XML data.

You can create an instance of the data conatiner in the class file and make this instance global.

require_once 'class-my_data_contaner.php';
global $data_container;
$data_container = new My_Data_Container;

// some other file
global $data_container;
$key1 = $data_container->key_one;

Or include the class everytime you need a value.

require_once 'path/to/class-my_data_container.php';
$data_container = new My_Data_Container();

$key1 = $data_container->key_one;

var_dump( $key1 );
var_dump( $data_container::$data );

In a oop script, a autoloader is best practise and you don't have to care about including the class everytime you need a value from it.

But all in all, it is not a real WordPress related topic. In Wordpress you normally read the propperties file, put the key/values in the database (if not already been done) and read the propperties from the database. You can use a transient if you don't want to care about cleanups after deactivating your theme/plugin.

  • 1
    +1 for giving such detailed answer, it's gave me a clear idea even for future. Mar 24, 2013 at 13:01

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.