7

I have been reading about the Database Description of Wordpress. I haven't understood the meaning to the use of the table wp_commentmeta.

The documentation says:

Each comment features information called the meta data and it is stored in the wp_commentmeta.

I have a fair idea of what meta-data is. I know how the table wp_postmeta stores custom field, for example. But in the case of the comments, I don't understand:

  • What kind of information would go in this table?

  • Why wouldn't it be in the same wp_comments table?

  • What is a practical example of how someone would use it, so that I could test it out and have a more graphic idea of how it works?

2 Answers 2

3

That table is essentially the same as for all of the other "meta" tables in the WordPress architecture. It holds misc. bits of extra, usually optional, information about the associated post, user, or in this case comment.

You can store whatever information you need to add to a comment-- perhaps a plugin wants to implement "abuse" flags, or comment upvotes. It can really be just about anything.

This information would not go in the comments table because it is usually optional and additional, and has no predefined meaning. How many extra columns would you put in the comments table "just in case"? See what I mean.

You can see an example of use in the Codex entry for add_comment_meta.

function add_custom_comment_field( $comment_id ) {

   add_comment_meta( $comment_id, 'my_custom_comment_field', $_POST['my_custom_comment_field'] );
}
add_action( 'comment_post', 'add_custom_comment_field' );
3
  • I see. It seems strange that they had a table "in case the plugins need it", without any core WP functionality using it. After all, the plugin could create its own table if the need it Feb 19, 2014 at 0:12
  • 1
    odd, maybe, but it follows the data structure of other tables that are used by the Core. Besides, there was a push awhile back to discourage plugins authors from creating tables as they tend not to get cleaned up properly when the plugin is deleted.
    – s_ha_dum
    Feb 19, 2014 at 19:53
  • These comments are very useful. I would like to add the observation that with this schema, there is the disadvantage that if you need to enforce the constraint that every comment foo needs exactly one comment meta bar there is no way to enforce it. You could have a comment with no bar or multiple bars in commentmeta if the table commentmeta somehow became corrupted. However, I see no better way to implement such functionality. Sep 10, 2014 at 16:09
3

This can be used, mainly by plugins, to add some additional information to a comment. By having one generic table you don't need to add columns to wp_comment for every additional piece of data.

E.g. a plugin could add a rating to each comment and store that value in wp_commentmeta.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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