I'm developing a plugin. At present I have two files. dotmailer.php and dotmailer-add.php. I'm creating a newsletter widget.

My dotmailer.php plugin adds an admin menu, with options. It allows the admin to store an email address which is used as authentication for the API I'm using. This is taken from the options page form and stored as such:

$apiemail = get_option('apiemail');

I've also created a widget, this is the front-facing bit. The user inputs their email in the form and the form POSTs to a new file called dotmailer-add.php. So this file has the variable $useremail taken from the form.

Now I'm stuck, because I need the var $useremail to be useable witin dotmailer-add.php. There is no way to send this through a form or anything because the form already posts through to the wordpress options.php file.

I know when working with PHP sessions are often used to store variables that are needed across multiple files. My issue is that the admin will not be submitting the API emal to the Wordpress options page every time they log in, so they won't be starting a new session each time. I need the var $useremail to be available across all my plugin files at all times.

I tried using include but unfortunately I get whitescreened with no errors (error messages are on).

How can I set a global variable through Wordpress options and have it available across multiple files within a plugin?

  • 1
    If this is only about implementing sessions into WordPress take a look at my answer about that. If you really want a global variable, likely should not be necessary, you should first take a look at the PHP Manual - Variable Scope. Aside from that you probably should add the code that you are working with, because seeing it it's much easier to give an specific answer. Sep 11, 2014 at 10:38
  • If WordPress sends through options.php file, why don't you retrieve the value via get_option('useremail') Or am I missing something? Sep 11, 2014 at 10:43
  • instead of processing the form in dotmailer-add.php you could put it in your widget. have the form submit to the current page and include a URL parameter that passes the value of the email address. have the widget code check for the existence of the parameter and perform the processing. this also allows you to replace the widget form with a 'thanks for signing up' message. Sep 11, 2014 at 11:39
  • possible duplicate of How to store and receive variables in WP sessions? Aug 2, 2015 at 11:48


Your Answer

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

Browse other questions tagged or ask your own question.