如何在发布WordPress评论时执行自定义操作?

时间:2012-09-11 00:23:28

标签: php wordpress cookies setcookie

我正在尝试在访问者提交评论时设置自定义Cookie,但我似乎无法使其正常工作。这是我在functions.php中的内容:

add_action('comment_post', 'ipro_set_comment_cookie');

function ipro_set_comment_cookie($comment) {
    setcookie("visitor_name", $comment->comment_author, time()+86400);
    setcookie("visitor_email", $comment->comment_author_email, time()+86400);
}

我也尝试将comment_post更改为wp_insert_comment - 似乎都没有效果。我正在研究WP的行动参考: http://codex.wordpress.org/Plugin_API/Action_Reference#Comment.2C_Ping.2C_and_Trackback_Actions

......任何想法?

1 个答案:

答案 0 :(得分:2)

查看Filter Reference

中的数据库写入

尝试comment_save_pre

之类的内容
  

在更新/编辑评论数据之前应用于评论数据。函数参数:注释数据数组,索引为“comment_post_ID”,“comment_author”,“comment_author_email”,“comment_author_url”,“comment_content”,“comment_type”和“user_ID”。

这样就设置了提交(所以它会在你的错误处理开始后调用)

如果我理解你的问题,这应该有效:

add_action('comment_save_pre', 'ipro_set_comment_cookie');

function ipro_set_comment_cookie($comment) {
    setcookie("visitor_name", $comment->comment_author, time()+86400);
    setcookie("visitor_email", $comment->comment_author_email, time()+86400);
}