in CMS by (9.9k points) edited by
0 like 0 dislike
1.4k views

How I can to create own custom variables in "wp-config.php" file and how I can to read variable values from "wp-config.php" file of WordPress CMS?

I would like to add some custom config variables in "wp-config.php" file (not in "functions.php"). Then, I need to access to these variables in my templates and plugins. What is the way to do that?

I need to get custom variables values from the "wp-config.php" file to use it in my plugins. Currently I have another file where I have this info, but I would like to only use the "wp-config.php". How can I read the different properties of "wp-config.php"?


wordpress get config variable value
wordpress set custom variable in config

1 Answer

0 like 0 dislike
by (9.9k points) edited by

In "wp-config.php" file you can set defines. They will be accesible everywhere without needing to use globals. Define something:

define('MY_DEFINE_NAME', 'THE_VALUE');

Then in your templates you can show the value like this:

echo MY_DEFINE_NAME;

Or create new variable and set to it a value:

$var = 'THE_VALUE';

But then need use "global" identifier in functions for point to certain variable (purely PHP feature, not WP).

This touch only themes. These custom defines and variables, what contain in config file, don't will accessible in plugins, from config will accesible only native WP defines and variables, such as "ABSPATH", and other. For plugins better use "functions.php" file for define custom variables and constants. I.e., do define own custom variables and constants, as shown above, but in "functions.php" file.

For check accessibility "functions.php" file in plugin you can use any function from "functions.php" file, e.g., function "current_time" so:

echo current_time('timestamp');

Also you can check accessibility native WP constants from "wp-config.php" file in plugin so:

echo ABSPATH;

Your answer

Try to answer the question as detailed as possible.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.