I want to add a field to the comments and have used these codes.
functions.php
function my_fields($fields) {
$fields['url2'] = '<p class="comment-form-url2">
<label for="url2">URL hittad på webben</label>
<input id="url2" name="url2" type="text" value="" size="30" />
</p>';
return $fields;
}
add_filter('comment_form_default_fields','my_fields');
comments.php
comment_form();
Questions
- It does not add an extra field in admin comment. Should it, or not?
- It does not seem to save the extra field. I tried to var dump the comment array. Why not?
There are a couple other hooks in the comment form that you can use. Where you’re hooking on only displays if the user isn’t logged in. If you want that field for all users (logged in or not), you need to add your form by hooking into both
comment_form_after_fields
andcomment_form_logged_in_after
, both of which are actions, and echo out the new field.Check out this tutorial I wrote (the example above is from it). Covers everything from adding fields to saving the data to adding a meta box so you can edit the extra fields on the back end too.
To save your extra field, you have to do :
See this nice tutorial covering extra fields in comment forms.
Take a look at this plugin
http://www.solaceten.info
(disclosure: I’m the co-author. The plugin is free, but not maintained any longer)