Wordpress ajax request and defined constants

时间:2015-05-04 19:49:56

标签: ajax wordpress request constants

I cannot access my custom defined constants (in functions.php) when I'm doing ajax request with:

add_action( 'wp_ajax_form_request', 'form_request' );
add_action( 'wp_ajax_nopriv_form_request', 'form_request' );

Accessible only standart WP constants, like TEMPLATEPATH.

Is possible access my own, defined in functions.php?

1 个答案:

答案 0 :(得分:1)

我刚刚测试过这样的东西并且工作得很好:

functions.php

define("MY_CONSTANT", "I am a man of constant sorrows.");

无论在哪里:

add_action( 'wp_ajax_form_request', 'form_request' );
add_action( 'wp_ajax_nopriv_form_request', 'form_request' );
function form_request()
{
    // check your nonce
    if($_POST['whatever'] == 'get_my_constant_or_whatever')
    {
        header("Content-Type: application/json");
        echo json_encode(MY_CONSTANT);
        exit;
    }
    else
    {
        // do something else or whatever
    }
}