I've created a plugin. In the initialisation file of my plugin I created an options page with some options to be stored by the admin.
<form method="post" action="options.php"> <?php settings_fields( 'settings-group' ); do_settings_sections( 'settings-group' ); ?> <label>API key</label><br /> <input type="email" name="apiemail" value="<?php echo get_option('apiemail'); ?>"> <p><input type="submit" value="Save" class="button-primary" /></p> </form>
This works correctly and options are saved.
In another file within the plugin folder, I need to get these options back and store them in variables.
$apiemail = get_option('apiemail');
However I get the error:
Fatal error: Call to undefined function get_option()
It seems this file has no concept of get_options. I can get it working if I include an abs path to
This then gets the options correctly. But the "require" of wp-config conflicts later on with me trying to do
Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/syyco/wp-config.php:85)
So how do I use
get_option in this file without
Edit: As requested, more detail regarding what I am trying to acheive.
I am creating a newsletter signup plugin which submits an email to an external API. The customer will see an input box, pop their email in and it will be added. Once it's added, I want to reroute them to a success page.
I have created a plugin which consists of two files. Dotmailer.php is my plugin initialisation file. Included is the creation of the plugin, the creation of an admin menu item and an options page. The options page has a bunch of admin login details needed by the API. This file also creates a widget (the front-facing part of the plugin) with the custom email input box.
The widget is pretty simple. It's a form with one input. It POSTs the email address data to another script called dotmailer-add.php, also in my plugin folder.
dotmailer-add.php takes the users email and sends it over to the API to be added to a mailing list. The API required an authorisation key. This auth key is one of the options I have asked the admin to set.
This is where the problem occurs. Dotmailer-add.php file needs to use
get_option to get the api key that the admin added through the dashboard. It cannot get this option, I get the error that get_option is undefined. If I
require wp-config.php, then get_option works fine.
You can see the code in it's entirety here.