0

I've created a function in my functions.php that's called when the submit_form event is fired on the front-end. Here is the code:

add_action( "wp_ajax_nopriv_submit_form", "submit_my_form" );
add_action( "wp_ajax_submit_form", "submit_my_form" );
function submit_my_form() {

    $send_to  = array( "test_address@test.com" );

    if ( is_page( "contact" ) ) {
        $subject = "This subject line is for the Contact page.";
    } else {
        $subject = "This subject line is for every other page.";
    }

    $message  = "Name: " . $_POST["name"] . "\r\n";
    $message .= "Email: " . $_POST["email"] . "\r\n";
    $message .= "Phone: " . $_POST["tel"] . "\r\n";
    $message .= "City: " . $_POST["city"] . ", " . $_POST["state"] . "\r\n";
    $message .= "Message: " . $_POST["message"] . "\r\n";

    wp_mail( $send_to, $subject, $message );
    wp_die();
} ?>

The problem is that I need my $subject and possibly $send_to variables to vary, based on the page. The answer to this question but it still doesn't address my problem. Is there a way to hack the is_page() function so it's functional within the ``functions.phpfile or is there a workaround that doesn't involve usingis_page()` at all?

  • 3
    Whatever data you want to operate on needs to be passed with the AJAX data. is_page will never work with an AJAX request, because it's not a page- you're making the request from a page, it's an entirely different http request. – Milo Aug 24 '15 at 16:36
  • 1
    to extend Milo's comment, you have to add the page name as an argument of the http request of the AJAX call – mmm Aug 24 '15 at 17:13
1

// You can retrieve your link and get the id (contact ID=4 for example)

$link = $_SERVER['HTTP_REFERER'];
$page_id = url_to_postid($link);

    if (page_id == '4'){
        $subject = "This subject line is for the Contact page.";
    } else {
        $subject = "This subject line is for every other page.";
    }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.