Custom Comment Form in WordPress

24th Jun 2011 at 10:08 AM | Posted in Wordpress | 6 Comments
<?php
// get defaults
add_filter('comment_form_defaults','mytheme_comments_form_defaults');
// get default fields
add_filter( 'comment_form_default_fields', 'comment_fields' );
// save extra fields in database
add_action( 'comment_post', 'save_comment' );

function mytheme_comments_form_defaults($default) {
	$default['comment_notes_after'] = 'You Can Add html code<br/><input type="checkbox" name="is_newsletter" id="is_newsletter" value="1"/>Subcsribe to our custom newsletter.<br/>';
	//unset($default['comment_notes_after']);
	return $default;
}
function comment_fields($fields)
{
	global $current_user;
	if(is_user_logged_in()){
		get_currentuserinfo();
		$phone = get_usermeta($current_user->ID,'business_phone');
		$address = get_usermeta($current_user->ID,'address');
	}
    $fields['url'] = '';
    $fields['location'] = '<p class="comment-form-location">' . '<label for="location">' . __( 'Location' ) . '</label> ' .
      '<input id="location" name="location" type="text" value="' . esc_attr( $address ) . '" size="30" /></p>';

    $fields['phone'] = '<p class="comment-form-phone">' . '<label for="phone">' . __( 'Phone' ) . '</label> ' .
      '<input id="phone" name="phone" type="text" value="' . esc_attr( $phone ) . '" size="20" /></p>';

    $fields = array( $fields['author'], $fields['location'], $fields['email'], $fields['phone'], $fields['url'] );
  return $fields;
}
function save_comment($comment_id)
{
	add_comment_meta( $comment_id, 'location', $_POST['location'], true );
	add_comment_meta( $comment_id, 'phone', $_POST['phone'], true );
	add_comment_meta( $comment_id, 'is_newsletter', $_POST['is_newsletter'], true );
}

?>

Output:

Front End:

Now Show the fields in admin section

<?php
//hook comment listing page
add_action('comment_text', 'show_commeta');
//hook edit comment
add_action( 'add_meta_boxes_comment', 'page_comments_status_meta_box' );
// save comment meta
add_action('edit_comment','save_markdown_after_edit');

if (is_admin()) {
   echo get_comment_text(), '<br/><span style="color:blue;">', get_comment_meta(get_comment_ID(), 'location',1).'</span>';
   }}

function page_comments_status_meta_box($post){

		$location = get_comment_meta($post->comment_ID,"location",true);
		$phone = get_comment_meta($post->comment_ID,"phone",true);
		$is_newsletter = get_comment_meta($post->comment_ID,"is_newsletter",true);

		if($post->user_id && ($location=='' && $phone=='')){
			$location = get_user_meta($post->user_id,"address",true);
			$phone = get_user_meta($post->user_id,"business_phone",true);
		}
?>
	<div class="stuffbox" id="namediv">
		<h3><label for="name">Other Details</label></h3>
			<div class="inside">
				<table class="form-table">
					<tr valign="top">
						<td class="first">Location:</td>
						<td><input type="text" id="location" tabindex="5" value="<?php echo $location; ?>" size="30" name="location"></td>
					</tr>
					<tr valign="top">
						<td class="first">
						Phone:</td>
						<td><input type="text" id="phone" tabindex="2" value="<?php echo $phone; ?>" size="20" name="phone"></td>
					</tr>
					<tr valign="top">
						<td class="first">
						Subscribe To Newsletter:</td>
						<td align="left" style="float:left;"><input type="checkbox" id="is_newsletter" tabindex="2" value="1" name="is_newsletter"<?php if($is_newsletter)echo "checked";?>></td>
					</tr>
				</table>
			<br>
		</div>
	</div>
<?php
}
function save_markdown_after_edit($comment_ID){
    if (isset($_POST['location'])){
        update_comment_meta($comment_ID,'location',$_POST['location']);
    }if (isset($_POST['phone'])){
        update_comment_meta($comment_ID,'phone',$_POST['phone']);
    }
}
?>

Output:

Admin End:

Advertisements

6 Comments »

RSS feed for comments on this post. TrackBack URI

  1. it looks really cool thanks 🙂

    • lol … I’m replying to my own comment .. I just wanna see how it looks like ans how the comments are organized 🙂

  2. whaat ?? where’s my facebook comment ?? it’s gone !!

    • Okay .. so this is a reply .. testing purposes 🙂

  3. noooooooooooooooooooooooo 😦 you didn’t explain anything …

    • dshfl jksd k


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.
Entries and comments feeds.

%d bloggers like this: