• 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)

    (@otto42)

    WordPress.org Admin

    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)

    (@otto42)

    WordPress.org Admin

    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

    (@3pinter)

    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)

    (@otto42)

    WordPress.org Admin

    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.