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

Written by  on January 20, 2017 

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"] . "rn";
    $message .= "Email: " . $_POST["email"] . "rn";
    $message .= "Phone: " . $_POST["tel"] . "rn";
    $message .= "City: " . $_POST["city"] . ", " . $_POST["state"] . "rn";
    $message .= "Message: " . $_POST["message"] . "rn";

    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 ^(http://www.netaksharee.com/goto/http://wordpress.stackexchange.com/questions/21418/why-isnt-is-page-working-when-i-put-it-in-the-functions-php-file) 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?

Category : Wordpress

Tags :