Intercept comment form submit/list by hook/filter

I am writing a media plugin that uses custom tables to store its contents. (eg not the post tables where wordpress stores it attachment data). Now i'm looking for a way to use the default wordpress comment system to add comments to it. (These comments will not be in the regular comment table but also a custom table.

I need two things:

  1. A hook that allows me to intercept the comment submit and process it with my own code if criteria are met. The criteria itself is arbitrary and not important, just need a way to get to the post data before Wordpress processes it.
  2. A filter that allows me to replace the wp_list_comments() data with my own tabledata.

It's a hacky idea, I know, but the post/attachment table is too limiting for what I need. Any ideas?

Topic hacks comments plugins Wordpress

Category Web


wp_list_comments() has no filters or hooks so its going to be a bit hard, what you can do is use comments_array filter hook

add_filter('comments_array','my_custom_comments_list');
function my_custom_comments_list($comments, $post_id){
    //if criteria are met
    //pull your comments from your own table
    //in to an array and return it.
    return $my_comments;
    //else return $comments
}

and as for "intercept the comment submit and process" chips answer would be the best way using preprocess_comment filter hook but you want be able to avoid WordPress form inserting the comment to the default table as well, so you can use wp_insert_comment action hook to remove the comment from the default table right after its inserted:

add_action('wp_insert_comment','remove_comment_from_default_table');
function remove_comment_from_default_table( $id, $comment){
    //if criteria are met
    //and the comment was inserted in your own table
    //remove it from the default table:
    wp_delete_comment($id, $force_delete = true);
}

Try using the preprocess_comment filter hook, e.g.

function my_handling_function( $comment_data ) {
     // Here, do whatever you need to do with the comment
     // modify it, pull data out of it to save somewhere
     // or whatever you need to do
     // just be sure to return $comment_data when you're done!
     return $comment_data;
}
add_filter( 'preprocess_comment', 'my_handling_function' );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.