0

I'm trying to update the user_id of a comment once it has been posted. It looks to see if the comment_author_email exists as a user in the users table, and if it does, attach that user to the comment.

function set_comment_author($comment_ID){
    global $wpdb;

    $comment = get_comment($comment_ID, ARRAY_A);
    if($comment["user_id"] == 0){
        $user = get_user_by('email', $comment["comment_author_email"]);
        if($user){
            $user_id = $user->ID;
            $comment["user_id"] = $user_id;
            wp_update_comment($comment); //This method doesn't work

            /*
             * This method doesn't work either
               $wpdb->update(   
                    $wpdb->prefix.'comments',
                    array(
                        'user_id' => $user_id
                    ),
                    array(
                        'comment_ID' => $comment_id
                    )
                );
            $wpdb->print_error();*/
        }
    }
}
add_action("comment_post", "set_comment_author", 10, 1);

I have done some debugging, and the action is being fired, the comment ID is coming through, and the user_id is being found. So the problem is updating the comment. Both wordpress function and database method don't work.

Any ideas?

0

Sorry, being stupid, i was using $comment_id, I needed $comment_ID.

The wp_update_comment() function still didn't work though, if anyone can shed some light onto that.

  • Edit your question instead of posting this as an answer. – Christine Cooper Jun 6 '13 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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