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

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.

Count clicks in your MySQL database

With this simple script it’s possible to count and track (country code and IP address) clicks from a visitors which followed a link to one of your link partners or advertisers. This script can be used together with your existing link database or, if you have made some modifications, with an array of links. I used the IP2nation database to get the visitors country code to store this code with the other information. We use the server variable HTTP_REFERER to store the URL, where the link was clicked, with the other data. You can use this script to cloak affiliate links too.

Use these SQL statements within your MySQL client:

CREATE TABLE `links` (
  `id` int(11) NOT NULL auto_increment,
  `titel` varchar(75) NOT NULL default '',
  `naam` varchar(35) NOT NULL default '',
  `url` varchar(150) NOT NULL default '',
  `description` text NOT NULL,
  PRIMARY KEY  (`id`)
) TYPE=MyISAM;

CREATE TABLE `clicks` (
  `id` int(10) NOT NULL auto_increment,
  `link_id` int(10) NOT NULL default '0',
  `visitor_ip` varchar(15) NOT NULL default '',
  `click_at` datetime NOT NULL default '0000-00-00 00:00:00',
  `country` char(2) NOT NULL default '',
  `on_page` varchar(50) NOT NULL default '',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM;

Install in the same way the data from ip2nation tables

The PHP code snippet

Create a script past this code between the PHP tags.

// place here your database connection code

if (isset($_GET['id']) && intval($_GET['id']) > 0) {
    $delay = 12*3600; // change here the number of hours how often a unique click must be counted
    $sql_check = sprintf("SELECT COUNT(*) AS test FROM clicks WHERE link_id = %d AND visitor_ip = '%s' AND UNIX_TIMESTAMP(click_at) + %d > UNIX_TIMESTAMP(NOW())", $_GET['id'], $_SERVER['REMOTE_ADDR'], $delay);
    if (mysql_result(mysql_query($sql_check), 0, "test") == 0) {
        $country_sql = "SELECT country FROM ip2nation WHERE ip < INET_ATON('".$_SERVER['REMOTE_ADDR']."') ORDER BY ip DESC LIMIT 0,1";
        $country = mysql_result(mysql_query($country_sql), 0, "country");
        $sql_insert = sprintf("INSERT INTO clicks (link_id, visitor_ip, click_at, country, on_page) VALUES (%d, '%s', NOW(), '%s', '%s')", $_GET['id'], $_SERVER['REMOTE_ADDR'], $country, $_SERVER['HTTP_REFERER']);
        mysql_query($sql_insert);
    }
    $sql_url = sprintf("SELECT url FROM link_table WHERE id = %d", $_GET['id']);
    $url = mysql_result(mysql_query($sql_url), 0, "url");
    header("Location: ".$url);
    exit;
} else {
    header("Location: http://www.yourwebsite.com/");
    exit;
}

How-to use the script in your web page?

The script above is your link target, you need to create links like:

<a href="http:domain.com/myclickscript.php?id=34">Click this link please</a>