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?
答案 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
}
}