In my plugin if I hard code an email address in my function with either wp_mail() or mail() I can get my form to work:

function email_form($ip_address, $agent, $referrer, $email, $name, $message) {
    $to         = "[email protected]";
    $subject    = "Hard Coded";
    $email_body = 
        "IP ADDRESS: "  . $ip_address   . "\n" . 
        "USER AGENT: "  . $agent        . "\n\n" .
        "EMAIL: "       . $email        . "\n" .
        "NAME: "        . $name         . "\n" .
        "MESSAGE: "     . $message      . "\n";
    mail("$to", "$subject", "$email_body", "From: $email");

However, when I try to add the email based on the author with:

$to = get_the_author_meta('user_email');

from a global $post; it will not send the email.

If I build a function at the beginning of the template and set a variable I can get the author to fire but I think this approach is incorrect. I can build a setting page but I want the recipient to be the author of the page. In a page template that sends the processing to another PHP file what is the proper way to pass the author's email of the page post type or any other WP related data (the_title(), theme, etc. etc) through WP_REST_Request?


After finding out I should go wp_localize_script I authored:

function foobar() {
    if (is_page_template('page-foo.php')) {
        wp_enqueue_script( 'bar_js', site_url() . '/js/bar.js', array('jquery'), '1.0.0', true);
        $localized = array(
            'url_string'    => site_url(),
            'form_id'       => get_the_ID(),
            'author_email'  => get_the_author_meta('user_email', get_post_field('post_author', get_the_ID())),
        wp_localize_script('bar_js', 'form_object', $localized);
add_action('wp_enqueue_scripts', 'foobar');

and passed in bar.js:

jQuery(document).ready(function($) {
    console.log('file is being detected'); // FOR TESTING

but is there a better way to call the author email outside of the loop in this manner?

  • What's the context here? Who is the author you're trying to get, the author of the post the form is on? Commented Sep 21, 2017 at 2:19

1 Answer 1


Why I Don't Have Access to Post's Information?

When you are using Admin-AJAX or the REST API, you will only have access to the current user's data (by that I mean by default, such as using is_user_logged_in()). To retrieve any other information, you have to pass it as an argument in your AJAX request. WordPress will not load the whole stuff in an AJAX request, so you have to find a workaround.

Using Hidden Inputs:

Since you don't have access to any post-related information (such as the post's ID), and approach would be to include a hidden input and add the extra data to it:

<input id="my-hidden-input" type="hidden" value="User ID Here" />

This goes anywhere in your page.php file. Now you can fetch it by using this simple jQuery command:

var userId = $('#my-hidden-input').val();

Localizing the Script:

There is a better alternative to do this, and it's by using wp_localize_script(). Let's say you are enqueuing your main JS file as follows:


You can pass the necessary data to your JS file, by localizing it. For example, we need to pass the post's ID. We can do so by creating a conditional and localizing our script:

if( is_page() ){
        'post_id' => get_the_ID(),
    wp_localize_script( 'my-js-handle', 'my_object_name', $localization_array, )'

Now we can access the post's ID in our script as simple as this:

window.alert( my_object_name.post_id );

Some Notes:

  • You should have an enqueued JS file to use the second method
  • The handle name of the localized script must be the same as enqueued script
  • You should localize your script "before" you enqueue it
  • 1
    Ah I totally forgot about wp_localize_script. I can pass other information with this, excellent. Will test later tonight and let you know.
    – user9447
    Commented Sep 21, 2017 at 16:47
  • @DᴀʀᴛʜVᴀᴅᴇʀ Haha finally found one of your questions on homepage!
    – Johansson
    Commented Sep 21, 2017 at 18:10
  • The problem with your edit is... "Which author?"? You are outside the loop. There is no author there.
    – Johansson
    Commented Sep 21, 2017 at 18:18
  • the author of the page template is what I'm after. It pulls the author when I console.log but I dont know if that is the proper approach outside the loop.
    – user9447
    Commented Sep 21, 2017 at 18:19

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.