2

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?

2 Answers 2

3

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);
}
2
  • Thank you both for your answers, this should get me going. I can only vote one answer so I picked the longest but I upvoted you both.
    – Patriek
    Commented May 6, 2011 at 17:05
  • If you use preprocess_comment, can't you strip out the custom content altogether, do with it what you need to do, and never return it to WordPress? Commented May 7, 2011 at 1:12
5

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' );
0

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.