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>

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 wp-config.php


This then gets the options correctly. But the "require" of wp-config conflicts later on with me trying to do header(Location:)

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 require of wp-config ?

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.

Dotmailer.php (the initialisation file) Dotmailer-add.php (the script that the widget form posts to)

1 Answer 1


You don't.

The get_option() function is a WordPress function. You cannot use it without loading WordPress.

More to the point, you should not be calling a file in your plugin directly. You should make your form or whatever is directly linking to that file link to a normal WordPress endpoint instead, and have additional parameters in your plugin to load that file from WordPress.

Essentially, you're doing it backwards. You're somehow running a file in your plugin and then having it load WordPress. But WordPress should be loading the plugin, not the other way around.

If you can explain what you're doing in more detail, perhaps somebody can tell you the correct approach to do that instead.

Edit: Given your additional information, I would invite you to consider this:

It POSTs the email address data to another script called dotmailer-add.php, also in my plugin folder.

That's your real problem here. You're sending information directly to a file in your plugin folder. This means that WordPress is not loaded because you bypassed it. So you have no access to the WordPress functions.

Take a look at this file in WordPress: /wp-admin/admin-post.php

You'll find this right at the top:

* WordPress Generic Request (POST/GET) Handler
* Intended for form submission handling in themes and plugins.

Sounds promising right? Using this is actually very easy.

Make a form that contains whatever form inputs you want, and a hidden "action". Something like this:

<form method="POST" action="<?php echo admin_url('admin-post.php'); ?>">
<input type="hidden" name="action" value="my_custom_action">
<input blah blah blah>

Now, make some code in your plugin that looks like this:

add_action('admin_post_nopriv_my_custom_action', 'my_custom_form_handler');
add_action('admin_post_my_custom_action', 'my_custom_form_handler');
function my_custom_form_handler() {
  ... stuff to handle the $_POSTed data goes here ...

And there you go. You can now handle your form within the WordPress context and have access to all it's functions. WordPress loads the plugin, the plugin does the processing if it has to do so.

If you need to access WordPress functionality, then your code needs to go through WordPress, not try to include it as a library. WordPress has endpoints like this for generic form handling, for javascript requests, for all sorts of things.

  • I have added a written explanation of what I am trying to achieve.
    – Francesca
    Sep 12, 2014 at 11:07
  • Can you suggest how one might use get_options in a plugin then? It shows as undefined when I use it.
    – Francesca
    Sep 12, 2014 at 11:12
  • Added an example for you.
    – Otto
    Sep 12, 2014 at 11:31
  • Thanks. I am going to give this a try now. I thought this might work but my issue was I did not know how to POST somewhere AND also create a custom action, I didn't think about making a hidden field. Thanks.
    – Francesca
    Sep 12, 2014 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.