Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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);
}
share|improve this answer
    
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 May 6 '11 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? –  Chip Bennett May 7 '11 at 1:12

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' );
share|improve this answer
    
Thank you for your answer as well –  Patriek May 6 '11 at 17:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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