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 using
is_page()` at all?
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 using
is_page()` at all?
1 Answer
Reset to default 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.";
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742319982a4421613.html
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