Problem Registering External API Key with Custom Plugin
-
Hi everyone,
I’m working on integrating a third-party API into my WordPress site, but I’m running into a problem with registering the API key properly.
Here’s what I’ve tried so far:
- I created a small custom plugin where I added a settings page for entering the API key.
- I’m using the WordPress Settings API (
register_setting,add_settings_section,add_settings_field) to store the key in the database. - The key seems to save correctly in the options table, but when I make API requests from the plugin, it looks like the key isn’t being recognized (the API returns “Invalid API key” error).
- If I hard-code the API key directly in the function, it works fine — so I think the issue is with how I’m registering or retrieving the saved option.
Here’s a snippet of how I’m calling it:
$api_key = get_option('my_plugin_api_key');
$response = wp_remote_get( "https://api.example.com/data?api_key=$api_key" );Has anyone else faced this kind of issue with storing/retrieving API keys through the WordPress Settings API? Am I missing something in the registration step or sanitization callback?
Thanks in advance for any guidance!
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
You must be logged in to reply to this topic.