Title: Hook a variable
Last modified: May 16, 2020

---

# Hook a variable

 *  [westham00](https://wordpress.org/support/users/westham00/)
 * (@westham00)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/hook-a-variable/)
 * Hello,
 * How can I hook a variable from a function and echo into a curl script. I have
   a script setup which runs when an email is sent, but I am unable to catch the
   variables and display in my curl script. I have attached code here. Thank you.
 * [https://stackoverflow.com/questions/61834516/hook-a-variable-from-an-email-wordpress](http://Stackoverflow here)
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fhook-a-variable%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

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

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/hook-a-variable/#post-12845708)
 * You could use the “wp_mail” filter, where the subject is passed to your callback.
   This fires before the email is sent, so you cannot know from here if the sending
   was successful or not. But what you could do is set the passed subject as a global
   variable in “wp_mail” that is later used in your dcart_line() callback.
 * Yes, globals are considered bad form by some, but when your action hook doesn’t
   pass the data you need there’s not many good choices. You could set a static 
   class property to avoid the nasty global. But it’s really just an obfuscated 
   global without the stigma 🙂
 *  Thread Starter [westham00](https://wordpress.org/support/users/westham00/)
 * (@westham00)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/hook-a-variable/#post-12850014)
 * Hi, thanks for your response. Sorry, I am quite new to this:
 * > You could use the “wp_mail” filter, where the subject is passed to your callback.
 * How can I achieve this within my code block, I am assuming this filter comes 
   directly underneath the `function dcart_line() {` , but when I add the wp_mail
   filter there is as an error. Thank you.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/hook-a-variable/#post-12853405)
 * No need to apologize, we were all new to this at one point. At least you’ve then
   been spared the use of globals micro-controversy among coders 🙂
 * The adding of “wp_mail” filter and its callback would be a completely separate
   code block. It can be in the same file, but not within your dcart_line() function.
   Don’t forget that in PHP a global variable must be declared both where it is 
   assigned and where it is used.

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

The topic ‘Hook a variable’ is closed to new replies.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [6 years, 2 months ago](https://wordpress.org/support/topic/hook-a-variable/#post-12853405)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
