‘COMMENT_POST’挂钩似乎不起作用?

时间:2015-07-22 作者:Bysander

This is driving me crazy - Does anyone know why this \'comment_post\' hook doesn\'t seem to work!? I\'ve basically written an entire email notification plugin for it and it\'s failed on the first step.

After research I was sure this was the right hook as it is after the comment is saved to the database.

add_action( \'comment_post\', \'test_notify\' );

function test_notify($args , $args2) {
    //test
    $headers = "MIME-Version: 1.0" . "\\r\\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\\r\\n";
    $email = \'[email protected]\';
    $message = \'Args=\' . $args . \'<br>Args2=\' . $args2;
    wp_mail( $email , \'test\', $message, $headers);

    echo \'It works!\';
    header("Location: /blog?thisworked=true");
}

When a comment is posted, the function is not triggered... at all? The do_action is on line 2095 of /wp-includes/comment.php

do_action( \'comment_post\', $comment_ID, $commentdata[\'comment_approved\'] );
1 个回复
SO网友:s_ha_dum

有人知道为什么这个“comment\\u post”挂钩似乎不起作用吗!?

。。。

发布评论时,不会触发该功能。。。完全

钩子起作用了。尝试以下方法,效果很好:

add_action( \'comment_post\', \'test_notify\' );
function test_notify($args , $args2) {
    echo \'It works!\'; 
    die;
}
如下所示:

add_action( \'comment_post\', \'test_notify\' );
function test_notify($args , $args2) {
    echo \'It works!\'; 
    header("Location: http://www.google.com");
    die;
}
事实上,这也很有效,但您可能会得到404:

add_action( \'comment_post\', \'test_notify\' );
function test_notify($args , $args2) {
    echo \'It works!\'; 
    header("Location: /blog?thisworked=true");
    die;
}
您想使用site_url()home_url() 可能:

add_action( \'comment_post\', \'test_notify\' );
function test_notify($args , $args2) {
    echo \'It works!\'; 
    header(\'Location: \'.home_url(\'/blog?thisworked=true\'));
    die;
}
如果这些都不起作用,就会有什么干扰,我不知道该怎么猜。

结束

相关推荐

Comments on future posts

我已启用未来(计划)帖子显示为单个帖子,以便启用页面评论,但在我尝试发布评论时出现问题。我得到一个没有显示任何警告或错误的空白屏幕?我想在评论尚未发布的帖子时会出现问题。有没有办法对未来的帖子发表评论?