Home > Mobile >  Undefined constant error in WordPress functions.php with add_action on rest_api_init
Undefined constant error in WordPress functions.php with add_action on rest_api_init

Time:11-05

Returned error is:

Fatal error: Uncaught Error: Undefined constant "initCors" in functions.php (in second to last line marked below).

I do not get this error in my local environment, but when I push to our WP Engine host, I get the error. The function initCors is defined in the first line, I do not understand why this is happening. I have used this same function on other websites.

function initCors ( $value ) {
    header( 'Access-Control-Allow-Origin: ' . esc_url_raw( site_url() ) );
    header( 'Access-Control-Allow-Methods: GET' );
    header( 'Access-Control-Allow-Credentials: true' );
    return $value;
}
add_action( 'rest_api_init', function() {
    remove_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' );
    add_filter( 'rest_pre_serve_request', initCors); //ERROR IN THIS LINE
}, 15 );

CodePudding user response:

Try replacing your code with this one.

add_filter( 'rest_pre_serve_request', 'initCors');
  • Related