Insert data on comment post

I am trying to find reference for comment_post to insert custom data to custom table when comment post.

I want to insert below things to my custom table

Read More
  1. Comment’s Post ID
  2. Custom content (which will done with custom query)

Can anyone please help me to do this?

Related posts

Leave a Reply

3 comments

  1. Not sure what you’re after but this is how you would get the content of a newly posted comment

    add_action( 'comment_post', 'my_comment_callback' );
    function my_comment_callback($id) {
        $comment = get_comments(array(
            'ID' => $id
        ));
    
        // $content is the actual text the user posted
        $content = $comment->comment_content;
    }
    

    Is that what you’re looking for?

  2. Your question is not very clear, but I think you are looking for the wp_insert_comment hook or one of the comment transitions hooks. Your question doesn’t allow for a more detailed answer. There just isn’t enough information.

    function insert_comment_extra_wpse_85096($cid) {
      // $cid is your comment ID
    }
    add_action('wp_insert_comment','insert_comment_extra_wpse_85096');
    

    See Also: http://codex.wordpress.org/Function_Reference/wp_transition_comment_status

  3. I have done this using comment_post action hook. Here is the answer if anyone need in future.

    add_action('comment_post', 'insert_gallery');
    function insert_gallery() {
    
        global $wpdb, $post;
        $post_id = $post->ID;
    
        $wpdb->insert(
            $wpdb->prefix. 'my_medias',
            array(
                'post_id' => $post_id,
                'image_name' => 'trial-image1',
                'status' => 1
            ),
    
            array(
                '%d',
                '%s',
                '%d'
            )
    
        );    
    
    }