Wordpress Warning: call_user_func_array() expects parameter 1 to be a valid callback, array must have exactly two members

前端 未结 1 1304
南旧
南旧 2021-01-14 13:15

I am trying to add a custom function that will add the Access-Control-Allow-Origin Header since I cannot access the .conf files on the server.

Below is

相关标签:
1条回答
  • 2021-01-14 13:55

    Well I figured it out. Hope this helps someone else.

    Since I am only calling a function without a method, I just removed the array () around my function name in the add_filter function. As you can see in Example #1 here, if you are only calling a function with arguments you only need to wrap your function name in single quotes.

    fixed code looks like:

    add_filter( 'wp_headers', 'eg_send_cors_headers', 10, 1 );
    function eg_send_cors_headers( $headers ) {
    
            $headers['Access-Control-Allow-Origin']= get_http_origin(); 
            $headers['Access-Control-Allow-Credentials'] = 'true';
    
    
            if ( 'OPTIONS' == $_SERVER['REQUEST_METHOD'] ) {
    
                if ( isset( $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] ) ) {
                    $headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS';
                }
    
                if ( isset( $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'] ) ) {
                    $headers['Access-Control-Allow-Headers'] = $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'];
                }
    
            }
    
        return $headers;
    }
    
    0 讨论(0)
提交回复
热议问题