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( "[email protected]" );

    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?

2
  • 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
    Commented Aug 24, 2015 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
    Commented Aug 24, 2015 at 17:13

1 Answer 1

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.";
    }

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.