Skip to content

Instantly share code, notes, and snippets.

@Tmeister
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tmeister/f6eaace5c2484cd6a603 to your computer and use it in GitHub Desktop.
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
<?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