在插件中添加Heartbeat API的过滤器-Wordpress

时间:2019-01-07 10:19:35

标签: wordpress

我想从WordPress Heartbeat API发出的请求中接收自定义值。 问题在于过滤器只能在functions.php中工作,而不能像往常一样在插件构造函数中工作。

我还有很多其他的钩子可以很好地工作。语法正确。我会误解加载顺序吗?

在插件构造函数中(无效):

add_filter( 'heartbeat_received', array( $this, 'test_heartbeat_received' ), 10, 2 );

function test_heartbeat_received( $response, $data ) {
    $response['test'] = 'test';
    return $response;
}

在functions.php中(工作):

add_filter( 'heartbeat_received', 'test_heartbeat_received', 10, 2 );

function test_heartbeat_received( $response, $data ) {
    $response['test'] = 'test';
    return $response;
}

我期望这个JSON响应:{test:“ test”,“ wp-auth-check”:true,server_time:1546856046}, 但只会得到{“ wp-auth-check”:true,server_time:1546856046}。

谢谢您的帮助!

编辑

我设法通过将过滤器放到主类之外来使其工作,问题可能与POO有关,但我不了解该过程...

add_filter( 'heartbeat_received', array(  new Test, 'test_heartbeat_received' ), 10, 2 );

1 个答案:

答案 0 :(得分:0)

与首页相关的我的课程已使用条件标记!is_admin()实例化,因此未触发所有AJAX操作。没有这种条件,它就可以工作!

相关问题