Title: add_shortcode to define global variable
Last modified: August 22, 2016

---

# add_shortcode to define global variable

 *  [3Pinter](https://wordpress.org/support/users/3pinter/)
 * (@3pinter)
 * [11 years, 8 months ago](https://wordpress.org/support/topic/add_shortcode-to-define-global-variable/)
 * I have my own template page which has some preset forms on ‘m. I would like to
   set the ‘to’-emailadres. This I can’t get working.
 * My thought: I create an add_shortcode so I can enter on my page: [set_email_to
   address=”funky@town.com”]
 * in my functions.php I have
 *     ```
       $GLOBALS['this_emailadres'] = null;
       function gotya_email ( $atts, $content = null ) {
       	extract(shortcode_atts(array("address" => "fallback@email.com"), $atts));
       	$GLOBALS['this_emailadres'] = $address;
       }
       add_shortcode('set_email_to address', 'gotya_email');
       ```
   
 * So I would be able to get my $GLOBALS[‘this_emailadres’] to be the emailaddress
   to send it to. Sadly it isn’t working.
 * Any thoughts on how I could better do this?
 * (yes I know I can install a plugin __ don’t go there 🙂 )

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [11 years, 8 months ago](https://wordpress.org/support/topic/add_shortcode-to-define-global-variable/#post-5271389)
 * add_shortcode doesn’t actually call that function until the shortcode is used
   somewhere on a page.
 * So your global won’t be set until after that shortcode has run, when `the_content()`
   is called to display your post. If you’re checking the global earlier than the
   call to the_content(), then it won’t be set yet.
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [11 years, 8 months ago](https://wordpress.org/support/topic/add_shortcode-to-define-global-variable/#post-5271390)
 * Oh, also, this is wrong:
 * `add_shortcode('set_email_to address', 'gotya_email');`
 * Should just be this:
 * `add_shortcode('set_email_to', 'gotya_email');`
 *  Thread Starter [3Pinter](https://wordpress.org/support/users/3pinter/)
 * (@3pinter)
 * [11 years, 8 months ago](https://wordpress.org/support/topic/add_shortcode-to-define-global-variable/#post-5271464)
 * Thanx Samuel for that small fix on my code (luckily I had it right in my functions.
   php __ didn’t use ctrl+c / ctrl+v ^^)
 * I forgot to mention that I use the_content() on my page_email.php (template).
 * But is it correct if I say:
 * “My page_email.php is loaded, I fill out the form, send which is on the same 
   page, so same page is loaded again. Because my email send handling is done in
   the first lines of my php file and afterwards my content (the_content() + custom
   html/code) my GLOBAL variable is lost”
 * If that is true, I perhaps should be better off storing that in a session?
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [11 years, 8 months ago](https://wordpress.org/support/topic/add_shortcode-to-define-global-variable/#post-5271498)
 * Or just move the email sending code to the end of the file?

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘add_shortcode to define global variable’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * Last activity: [11 years, 8 months ago](https://wordpress.org/support/topic/add_shortcode-to-define-global-variable/#post-5271498)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
