Sean Davis
Sean Davis

Reputation: 19

Is it possible to define an array in wp-config.php

wp-config.php

$array = array("test1","test2");
define("TEST", $array);

testFile.php

$array = TEST;

Is this possible? The above code merely sets $array to "TEST". Or is there a way to retrieve values from wp-config.php by string i.e. something like:

$array_first_value = wp_get_config("array_first_value");

Upvotes: 1

Views: 1516

Answers (2)

Vortexs
Vortexs

Reputation: 121

You can serialize an array for use in a constant and unserialize it later when you need it.

So, in wp-config.php, you could do the following:

$array = array( "testvalue1", "testvalue2" );
define( "TEST", serialize($array) );

And then in some other file:

$array = unserialize(TEST);

However, constant are not meant to hold arrays. So, you might want to rethink your need for a constant array.

Upvotes: 2

Nathan Dawson
Nathan Dawson

Reputation: 19308

Array values can't be used with define. You can only use scalar (int, float, string, bool) or null.

Please see: http://php.net/manual/en/function.define.php

Upvotes: 1

Related Questions