Is there a hack for using is_page() within the function.php file?

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

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?

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?

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Aug 24, 2015 at 16:30 WilhelmWilhelm 1913 silver badges8 bronze badges 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
Add a comment  | 

1 Answer 1

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 there a hack for using is_page() within the function.php file?

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

    8小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信