by

You may be familiar with the syntax of writing code-Tags like some code here. This is what is used by Github for example.

At Daring Fireball they wrote up the syntax. Not only for code-Tags but for various features that are connected to style and semantics.

In the comments of this blog it is possible to use some of the rules to add some markup to them.

For instance when you write:

Some fancy text with **styled _markup_** and `code`.

It will be transfered to

Some fancy text with <strong>styled <em>markup</em></strong> and <code>code</code>.

I really like to use these tags in comments to style them a little bit.

Use in your own intallation

So if you want to use them in your own WordPress-installation check the following function – paste it in your functions.php:

function my_preprocess_comment( $comment ) {
  // Replace all appereances of __ and ** with <b> and <strong>
  $comment['comment_content'] = preg_replace( '/__(.*?)__/is', '<b>$1</b>', $comment['comment_content'] );
  $comment['comment_content'] = preg_replace( '/\*\*(.*?)\*\*/is', '<strong>$1</strong>', $comment['comment_content'] );
 
  // Replace all appereances of _ and * with <i> and <em>
  $comment['comment_content'] = preg_replace( '/_(.*?)_/is', '<i>$1</em>', $comment['comment_content'] );
  $comment['comment_content'] = preg_replace( '/\*(.*?)\*/is', '<em>$1</em>', $comment['comment_content'] );
 
  // Replace all appereances of ` with <code>
  $comment['comment_content'] = preg_replace( '/`(.*?)`/is', '<code>$1</code>', $comment['comment_content'] );</code>
 
  return $comment;
}

You also need to add a filter to get the function working.

add_filter('preprocess_comment', 'my_preprocess_comment');

This is everything that’s necessary to get the style-tags for all of your comments. It’s pretty easy and you may help people that want to comment without thinking about HTML and stuff.
Even though if you think that it’s fine for users to use HTML if they want to style their comments and don’t want to add this function: Add this snipped anyway as it does not affect any existing functions in WordPress and the comment-section. It’s just an add-on.

Hans Reinl

Written by Hans Reinl

Hans is a VP of Engineering with a passion for technology and a focus on leading and developing strong engineering teams. Committed to delivering high-quality products and driving business growth through technical excellence.