$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>comments - How to check, if user commented before, on comment_post action?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

comments - How to check, if user commented before, on comment_post action?

matteradmin8PV0评论

I want check, if user commented post, in comment_post action. So i make this:

function checkUserComment( $comment_ID ) {

    $comment = get_comment( $comment_ID );
    $postID = $comment->comment_post_ID;
    $authorID = $comment->user_id;

    // If user commented this post already, don't update post meta.
    if( get_comments( array( 'post_id' => $postID, 'user_id' => $authorID ) ) )
        return;

    update_post_meta($postID, 'testMeta', 'testValue');

}

add_action( 'comment_post', 'checkUserComment', 10, 2 );

It must doesn't update post_meta if user comments first time. But it updates post_meta anyway.

I want check, if user commented post, in comment_post action. So i make this:

function checkUserComment( $comment_ID ) {

    $comment = get_comment( $comment_ID );
    $postID = $comment->comment_post_ID;
    $authorID = $comment->user_id;

    // If user commented this post already, don't update post meta.
    if( get_comments( array( 'post_id' => $postID, 'user_id' => $authorID ) ) )
        return;

    update_post_meta($postID, 'testMeta', 'testValue');

}

add_action( 'comment_post', 'checkUserComment', 10, 2 );

It must doesn't update post_meta if user comments first time. But it updates post_meta anyway.

Share Improve this question edited Dec 23, 2018 at 12:31 wpdev asked Dec 23, 2018 at 11:05 wpdevwpdev 5492 gold badges13 silver badges28 bronze badges 4
  • To narrow the problem you have to check each variable values, e.g., by echoing it. – Max Yudin Commented Dec 23, 2018 at 11:15
  • I checked all variables and print all them. Also tried is_array() and !empty() but same. I wonder, when action check it? After comment submisson or before? – wpdev Commented Dec 23, 2018 at 11:25
  • And what is the problem exactly? You say it doesn’t work. How? – Krzysiek Dróżdż Commented Dec 23, 2018 at 11:59
  • Updated question. – wpdev Commented Dec 23, 2018 at 12:32
Add a comment  | 

1 Answer 1

Reset to default 1

From the WordPress Codex:

comment_post is an action triggered immediately after a comment is inserted into the database.

Instead, I think what you want to try is to hook into preprocess_comment and not comment_post.

Post a comment

comment list (0)

  1. No comments so far