WordPress snippets, hacks and tips to enhance your comments section

Allow more HTML tags in WordPress comments

By default, WordPress allow commenters to use some HTML tags in the comment textarea. But depending on your blog, you might want to allow the use of more tags.

To do so, simply paste the code below into your functions.php file. The list of allowed tags can be modified on line 4.

// Create function which allows more tags within comments
function allow_pres() {
  global $allowedtags;
  $allowedtags['pre'] = array('class'=>array());
}

// Add WordPress hook to use the function
add_action('comment_post', 'allow_pres');

» Source: http://davidwalsh.name/wordpress-comment-tags

Remove autolinks in comments

When a commenter paste a url link into the comment form, WordPress automatically transform it into a hypertext link. This is good on most cases, but personally I do not like this as many people used my blog comments to advertise their own products and services.

Removing autolinks in comments is super easy: Just insert the line of code below into your functions.php file.

remove_filter('comment_text', 'make_clickable', 9);

» Source: http://www.wprecipes.com/wordpress-hack-remove-autolinks-in-comments

Remove the url field from WordPress comments form

If you don’t want your commenters to be able to enter their website url, here is a snippet to remove the url field from WordPress comment form.

Simple paste the code below in your functions.php file, save it, and you’re done.

function remove_comment_fields($fields) {
    unset($fields['url']);
    return $fields;
}
add_filter('comment_form_default_fields','remove_comment_fields');

» Source: http://wp.tutsplus.com/tutorials/creative-coding/customizing-comments…

Add more fields to the comments form

I just shown you how to remove a field from the comment form, but now, what if you want to add a new field to the form? The following example will add a new field for the commenter to add his age.

Let’s start by adding the field. To do this, copy the code below and paste it in your functions.php file:

function add_comment_fields($fields) {
 
    $fields['age'] = '<p class="comment-form-age"><label for="age">' . __( 'Age' ) . '</label>' .
        '<input id="age" name="age" type="text" size="30" /></p>';
    return $fields;
 
}
add_filter('comment_form_default_fields','add_comment_fields');

function add_comment_meta_values($comment_id) {
 
    if(isset($_POST['age'])) {
        $age = wp_filter_nohtml_kses($_POST['age']);
        add_comment_meta($comment_id, 'age', $age, false);
    }
 
}
add_action ('comment_post', 'add_comment_meta_values', 1);

Now, to display the age of the commenter, use the following code:

<?php echo "Comment authors age: ".get_comment_meta( $comment->comment_ID, 'age', true ); ?>

» Source: http://wp.tutsplus.com/tutorials/creative-coding/customizing-comments…

Insert comments programatically

On some specific cases, you might want to insert comments programmatically. Doing so is definitely easy, as shown below. Simply execute this code and it will add a new comment in your database.

$data = array(
	'comment_post_ID' => 1,
	'comment_author' => 'admin',
	'comment_author_email' => '[email protected]',
	'comment_author_url' => 'http://www.catswhocode.com',
	'comment_content' => 'Lorem ipsum dolor sit amet...',
	'comment_author_IP' => '127.0.0.1',
	'comment_agent' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; fr; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3',
	'comment_date' => date('Y-m-d H:i:s'),
	'comment_date_gmt' => date('Y-m-d H:i:s'),
	'comment_approved' => 1,
);

$comment_id = wp_insert_comment($data);

» Source: http://www.wprecipes.com/wordpress-hack-insert-comments-programatically

Automatically disable commenting on posts older than X days

If your blog is very popular, you probably receive lots of comments, some on very old posts… So why not automatically disable commenting on posts older than X days?

Paste the code below in your functions.php file. The code will disable comments on post older than 30 days. The amount of days can be changed on line 3.

function close_comments( $posts ) {
	if ( !is_single() ) { return $posts; }
	if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( 30 * 24 * 60 * 60 ) ) {
		$posts[0]->comment_status = 'closed';
		$posts[0]->ping_status    = 'closed';
	}
	return $posts;
}
add_filter( 'the_posts', 'close_comments' ); 

» Source: http://perishablepress.com/wordpress-tip-disable-comments-in-old-posts-via-php/

Add target=”blank” to all links in comment text

Using the HTML target="blank" attribute is generally discouraged as this force the opening of a link in a new tab or window, but I know how clients like it. So if you need to add a target="blank" attribute to every links in comments text, just paste the code shown below in your functions.php and you’ll get the job done.

function autoblank($text) {
	$return = str_replace('<a', '<a target="_blank"', $text);
	return $return;
}
add_filter('comment_text', 'autoblank');

» Source: http://www.catswhocode.com/blog/snippets/add-target_blank-on-all-link

Customize comments markup

By default, WordPress outputs a standard comments list, but if can be customized if needed. Paste the following code into your functions.php file and customize as desired.

function my_custom_comments($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment; ?>
   <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
   <?php if ($comment->comment_approved == '0') : ?>
      <em><?php _e('Your comment is awaiting moderation.') ?></em>
   <?php endif; ?>

   // Comments markup code here, e.g. functions like comment_text(); 

}

Once done, use the following line of code in comments.php to display your custom comment markup:

<?php wp_list_comments("callback=my_custom_comments"); ?>

» Source: http://css-tricks.com/snippets/wordpress/customize-comments-markup/

Disable comments feeds in individual posts

By default, WordPress create RSS feeds for all individual posts. If you don’t want or need them, you can disable them with the following code snippet. Simply paste it in your functions.php file and you’ll get rid of individual post feeds.

// disable comment feeds for individual posts
function disablePostCommentsFeedLink($for_comments) {
	return;
}
add_filter('post_comments_feed_link','disablePostCommentsFeedLink');

» Source: http://digwp.com/2009/11/disable-comment-feeds-individual-posts/

Want more WordPress tips and snippets? Then have a look to WPRecipes, another blog of mine!

Leave a Reply

Your email address will not be published. Required fields are marked *