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:
<?php
/*
* 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 ) )
$this->read_propperties();
}
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;
else
$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.