Add a field to the WordPress comment form

Source: Internet
Author: User

Problem Description

Friends of wp all know that there are only four wordpress comment form fields, including nicknames, email addresses, URLs, and content. Comments are included in the wp_comments table, if you want to add other additional fields, you have to find another method, for example, tel phone field.
Solution
Wp_comments also has a corresponding meta table named wp_commentmeta. The Posts article table can use postmeta to store some additional information, so the comment can also use commentmeta to store additional comment fields. If you have installed a comment plug-in, you can look at the wp_commentmeta table. The content is also stored here. This table has four fields: meta_id, comment_id, meta_key, and meta_value, take the following screenshot:

The storage format is the same as above. duoshuo_post_id is the name of the field, and meta_value stores the content.
How to add custom fields to a comment form
 
1. Add the desired field to the comments. php comment form, for example:

<P>
<Input type = "text" name = "tel" id = "tel" size = "22" tabindex = "4"/>
<Label for = "tel"> Phone number </label>
</P>

Why is the tabindex attribute in the descending order? You can try it on your own ....
 
2. If the comment form comes with the system, use the following method to add the form field. If not, skip


Add_filter ('comment _ form_default_fields ', 'comment _ form_add_ewai ');
Function comment_form_add_ewai ($ fields ){
$ Label1 = _ ('Country/region ');
$ Label2 = _ ('Skype account ');
$ Label3 = _ ('telephony ');
$ Label4 = _ ('faxed ');
$ Label5 = _ ('address ');
$ Value1 = isset ($ _ POST ['guojia '])? $ _ POST ['guojia ']: false;
$ Value2 = isset ($ _ POST ['Skype '])? $ _ POST ['Skype ']: false;
$ Value3 = isset ($ _ POST ['tele'])? $ _ POST ['tel']: false;
$ Value4 = isset ($ _ POST ['fax'])? $ _ POST ['fax']: false;
$ Value5 = isset ($ _ POST ['address'])? $ _ POST ['address']: false;
$ Fields ['guojia '] = <HTML
<P>
<Label for = "guojia" >{$ label1} </label>
<Input id = "guojia" name = "guojia" type = "text" value = "{$ value1}" size = "30"/>
</P>
HTML;
Return $ fields;
}
 
3. Receive form fields and write them to the database

Add the following code to functions. php in the topic directory:

Add_action ('WP _ insert_comment ', 'WP _ insert_tel', 10, 2 );
Function wp_insert_tel ($ comment_ID, $ commmentdata ){
$ Tel = isset ($ _ POST ['tele'])? $ _ POST ['tel']: false;
// _ Tel is the field name stored in the database.
Update_comment_meta ($ comment_ID, '_ tel', $ tel );
}

You can write data to the database in these two steps.
The values 10 and 2 in the add_action () parameter indicate that the priority of the function execution is 10 (the default value, the smaller the value, the higher the priority). The Function accepts two parameters.
 
4. Display additional fields in the background

The first two steps are only for receiving and writing data to the database. How can this problem be displayed in the background comment list? Copy the following code to functions. php in the topic directory:

Add_filter ('manage _ edit-comments_columns ', 'My _ comments_columns ');
Add_action ('manage _ comments_custom_column ', 'output _ my_comments_columns', 10, 2 );
Function my_comments_columns ($ columns ){
$ Columns ['_ tel'] = _ ('telephony '); // The phone number indicates the column name.
Return $ columns;
}
Function output_my_comments_columns ($ column_name, $ comment_id ){
Switch ($ column_name ){
Case "_ tel ":
Echo get_comment_meta ($ comment_id, '_ tel', true );
Break;
}

If you want to call it in the message list at the front end, use the following code. _ tel is the field name you store in the database.

<? Php
$ Tel = get_comment_meta ($ comment-> comment_ID, '_ tel', true );
If (! Empty ($ tel )){
Echo "phone". $ tel;
}
?>
 
5. Let's see if there is an additional phone number in the comment list in the background. That's all right.

 
6. To remove a built-in form field, use the following code:

Function tel_filtered ($ fields ){
If (isset ($ fields ['tele'])
Unset ($ fields ['tel']);
Return $ fields;
}
Add_filter ('comment _ form_default_fields ', 'tel ')

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.