-
-
Save Tmeister/f6eaace5c2484cd6a603 to your computer and use it in GitHub Desktop.
Simple WordPress Plugin to remove the Email Field from the comments form and add a Phone Field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Comments Fields | |
Description: Remove the Email field and add a Phone field | |
Plugin URI: http://enriquechavez.co | |
Author: Enrique Chavez | |
Author URI: http://enriquechavez.co | |
Version: 1.0 | |
License: GPL2 | |
*/ | |
/** | |
* Modify the front-end form | |
**/ | |
function modify_fields($fields) | |
{ | |
/** | |
* Remove Email Field | |
**/ | |
if( isset($fields['email']) ){ | |
unset($fields['email']); | |
} | |
/* | |
* Add the field in the form | |
*/ | |
$fields[ 'phone' ] = '<p class="comment-form-phone">'. | |
'<label for="phone">' . __( 'Phone' ) . '</label>'. | |
'<input id="phone" name="phone" type="text" size="30" /></p>'; | |
return $fields; | |
} | |
add_filter('comment_form_default_fields', 'modify_fields'); | |
/** | |
* Save the new field value | |
**/ | |
function save_comment_extra_fields($comment_id) | |
{ | |
if ( ( isset( $_POST['phone'] ) ) && ( $_POST['phone'] != '') ){ | |
$phone = wp_filter_nohtml_kses($_POST['phone']); | |
add_comment_meta( $comment_id, 'phone', $phone ); | |
} | |
} | |
add_action( 'comment_post', 'save_comment_extra_fields' ); | |
/** | |
* Adding a new column in the comment list (Admin) | |
**/ | |
function add_comment_columns( $columns ) | |
{ | |
$columns['phone'] = __( 'Phone' ); | |
return $columns; | |
} | |
add_filter( 'manage_edit-comments_columns', 'add_comment_columns' ); | |
/** | |
* Populate the new column with the field data. | |
**/ | |
function add_comment_phone_column($column, $comment_id) | |
{ | |
if( $column == 'phone' ){ | |
if( $phone = get_comment_meta( $comment_id, $column, true ) ){ | |
echo $phone; | |
} | |
} | |
} | |
add_filter( 'manage_comments_custom_column', 'add_comment_phone_column', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment