Where can I store API keys in WordPress? I need to access them in the root directory of the site, and for some reason adding them to wp config makes them inaccessible in the root directory.
There's a file calledwp-config.php
that's ordinarily in the root directory of a WordPress installation.
It's a PHP program, and has entries like these in it.
define('SECURE_AUTH_KEY', '9LjsBztwN9VuM=Fl=V6h8lM1zF@&o=-6W]7`VLkIjZgbXYPx');
define('NONCE_KEY', '*oc2{8Hj+nElTjL_Tf9#o2BC|y5kxRD+;&q_H3P+0v6MDeGM');
This is php code: in any WordPress code (plugin, theme) running in that installation you'll be able to useSECURE_AUTH_KEY
constant. So, you could put your own
define('MY_API_KEY', 'whatever-whatelse-o2BC|y5kxRD+;&q_H3P+0v6MDeGM');
in there and then use yourMY_API_KEY
constant in any code you wish.
If you're managing an API with a plugin or other code, you can put your key into the database. From WordPress code that would be done with
update_option( 'MY_API_KEY', 'whatever-whatelseC|y5yadda-yadda-eGM', false );
and retrieved with
$key = get_option( 'MY_API_KEY' );
This is the sort of thing you'll do if your plugin's settings panel prompts the site owner for the key.
If you have wp-cli installed on your machine, a shell script running in the site's root directory can get the key this way.
wp db query "SELECT option_value FROM wp_options WHERE option_name = 'MY_API_KEY'"
If none of these choices work for you, perhaps you could update your question or ask another.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.