duancheng6221 2014-08-12 16:39
浏览 182
已采纳

Wordpress警告:call_user_func_array()期望参数1是有效的回调,数组必须只有两个成员

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 my code;

add_filter( 'wp_headers', array( '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;
}

I then get this error when I save:

Notice: Undefined offset: 1 in /example/wp-includes/plugin.php on line 873 Warning: call_user_func_array() expects parameter 1 to be a valid callback, array must have exactly two members in /example/wp-includes/plugin.php on line 192

  • 写回答

1条回答 默认 最新

  • doudiao2335 2014-08-12 16:59
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100