Shorten text values without to break words

There are several situations were a shorter text version on your website is necessary: Post summaries for result pages or just for the page’s META description (of course it’s better to write a unique description). The regular PHP function “substr()” will shorten your text to a given length without a check if the text will end in a middle of a word.

Continue reading Shorten text values without to break words

How-to fix the hReview snippet for GD Star Ratings

Recently it becomes much easier to get your rating into the search results of Google. Using hReview rich snippet code in your blog post can raise the CTR in Google if the rating is visible. The GD Star Ratings plugin has already a function to show the snippet beside the stars widget, but the markup is according the Google Webmaster Help section not valid. We wrote a small function that will fix this problem.

Continue reading How-to fix the hReview snippet for GD Star Ratings

Remove HTML comments in WordPress

If you check the HTML source from your WordPress website, you will notice a lot of HTML comments. These comments are not visible for visitor and can help the WordPress theme developer while working on his HTML code. Some of you would say: “debugging information shouldn’t be visible to the client”.
There are also plugin developers which place this kind of comments into the code to “mark” their work.

The following WordPress actions are able to filter your code from all HTML comments. Place the PHP code into your functions.php file inside the WordPress theme directory.

function callback($buffer) {
    $buffer = preg_replace('/<!--(.|s)*?-->/', '', $buffer);
    return $buffer;
}
function buffer_start() {
    ob_start("callback");
}
function buffer_end() {
    ob_end_flush();
}
add_action('get_header', 'buffer_start');
add_action('wp_footer', 'buffer_end');

This kind of output buffering is more memory intensive and might raise your server’s load. Use always a WordPress cache plugin.

Show WordPress comments by category only

Recently I needed a widget or plugin which is able to show only the comments from th current category. It seems that this kind of widget is not very common, so I checked the code from several themes I used in the past and also some plugin. The function below should do the trick, place this function into your functions.php file (theme directory) and add the function into your sidebar.

Features included:

  • Shortened comments snippet
  • the comment author’s name
  • filter by one or more category IDs
  • shorten the post title if needed
  • Show the comment date (local date format)

Inside this function another custom function is called, include that function too:

How-to use the current category ID inside that function?

$myCategory = get_the_category();
$catId = array($myCategory[0]->cat_ID);
echo my_recent_comments(10, 100, $catId);

The SQL statement inside the comment function is based on a plugin I found on the WordPress website.