0

I wrote a function to update the comment time when a comment goes form unapproved to approved:

function ji_update_comment_date($comment){
$commentarr = array(
                'comment_ID'=>comment_ID(),
                //this is where it fails me,I can't get the ID
                'comment_date'=>date("Y-m-d H:i:s"),
                'comment_date_gmt'=> date("Y-m-d H:i:s") 
                );

        wp_update_comment( $commentarr );}
add_action('comment_unapproved_to_approved','ji_update_comment_date');

SO the comment_ID() is not working ,I can't seem to get it otherwise my code works perfectly.

9
  • Hi, Have you tried get_comment_ID() instead because comment_ID() must be within a loop.
    – Johansson
    Commented Jan 26, 2017 at 4:43
  • yeah,I tried ,it's not working neither.
    – 吉 宁
    Commented Jan 26, 2017 at 4:45
  • 1
    Oddly enough, get_comment_ID() worked for me with JS disabled. Using the suggestion from @JackJohansson ( adding $comment = get_comment( $comment ); see wordpress.stackexchange.com/a/45195/2807 ) and using 'comment_ID' => $comment->comment_ID, ended up being the real solution. Commented Jan 26, 2017 at 5:30
  • 2
    @DaveRomsey yes, i'm wondering why it's not working in his case. There might be a function or code before it, changing the results since $comment->comment_ID should absolutely work.
    – Johansson
    Commented Jan 26, 2017 at 5:34
  • 1
    Thank you guys ,it works now ,thank you for the clarity @Dave Romsey,it should be 'comment_ID' => $comment->comment_ID instead of what I used 'comment_ID'=>$comment->ID.Thank you for the get_comment tip Jack Johansson.
    – 吉 宁
    Commented Jan 26, 2017 at 6:02

1 Answer 1

1

So @Jack Johansson and @Dave Romsey helped me to make it work.So I will put the solution here:

function ji_update_comment_date($comment,$commentarr){

$comment = get_comment( $comment );
$commentarr = array(
                'comment_ID'=>$comment->comment_ID,
                'comment_date'=>date("Y-m-d H:i:s"),
                'comment_date_gmt'=> date("Y-m-d H:i:s") 
                );

        wp_update_comment( $commentarr         );}
add_action('comment_unapproved_to_approved','ji_update_comment_date');

I don't understand why comment_ID() and get_comment_ID() won't work in this scenario. But get_comment is surely a workaround.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.