by Richard Dows on Monday, 15 August 2011. |
No Comments |
After having tried a couple of URL shorteners in several themes, I thought I'd pass along the work so others can use it.
The ones I have so far are:
Bit.ly
function getBitlyUrl() {
//login information
$url = get_permalink(); // generates wordpress' permalink
$login = 'jbbrwcky'; // your bit.ly login
$apikey = 'R_5e1afc5207fce1beb266c01ad9694aa9'; // bit.ly apikey
$format = 'json'; // choose between json or xml
$version = '2.0.1';
// create the URL
$bitly = 'http://api.bit.ly/shorten?version='. $version .'&longUrl='. urlencode($url) .'&login='. $login .'&apiKey='. $apikey .'&format='. $format;
// get the url
$response = file_get_contents($bitly);
// parse depending on desired format
if (strtolower($format) == 'json') {
$json = @json_decode($response,true);
return $json['results'][$url]['shortUrl'];
} else {
$xml = simplexml_load_string($response);
return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
}
}
Tiny.Url
function getTinyUrl() {
$url = get_permalink();
$tinyurl = file_get_contents("http://tinyurl.com/api-create.php?url=". $url);
return $tinyurl;
}
Goo.gl
function getGoogle($url, $post_id) {
global $post;
if (!$post_id && $post) $post_id = $post->ID;
if ($post->post_status != 'publish')
return "";
$shortlink = get_post_meta($post_id, '_googl_shortlink', true);
if ($shortlink)
return $shortlink;
$permalink = get_permalink($post_id);
$http = new WP_Http();
$headers = array('Content-Type' => 'application/json');
$result = $http->request('https://www.googleapis.com/urlshortener/v1/url', array( 'method' => 'POST', 'body' => '{"longUrl": "' . $permalink . '"}', 'headers' => $headers));
$result = json_decode($result['body']);
$shortlink = $result->id;
if ($shortlink) {
add_post_meta($post_id, '_googl_shortlink', $shortlink, true);
return $shortlink;
} else {
return $url;
}
}
Su.Pr
function getSuPr() {
$url = get_permalink();
$supr = file_get_contents("http://su.pr/api?url=". $url);
return $supr;
}
Hooking It Up
To wp_get_shortlink() that is. Somewhere after your url shortener code, add this bit of code into the functions.php file, replacing 'your_function_name' with the name of the function you want to use.
add_filter('get_shortlink', 'your_function_name', 9);
Then to use the code, put this into the theme files of your choice.
echo "Shortlink: " . wp_get_shortlink();
by Richard Dows on Friday, 12 August 2011. |
No Comments |
Responding to a need, this is a small widget that lets the user select the categories to show with the posts that belong to each category. The category can be a single category (by ID: 4) or a comma-delineated list of categories (by ID: 4,7,10).
This is a modification of the widget/plugin provided by Transformation Power Tools, which provided some category functionality but not what we were looking for at the time. Nevertheless, a big thanks is due to them.
Admin/Widget Options

As you can see from the admin options, you can select how many posts to show in each category, to show the post count, the title of the widget, and also which categories to display with their posts.
To show a category just list it by ID (i.e., '4'), to show multiple categories list them by a comma-delineated list of IDs (i.e., '4,7'). Future versions may include a dropdown list.
Widget Appearance

The appearance shows the Categories and Posts Widget in action, in this case showing two categories with their posts. This also shows something else, that a post will only be shown once; in this particular case one of the posts is in both categories, but shown just once.
Download
Download the Categories & Posts Widget. Install in the wp-content/plugins/ directory, then activate in the Plugins section in your WordPress admin backend.
To use it, go into the Widgets section, then drag into your sidebar of choice.
by Richard Dows on Friday, 29 July 2011. |
No Comments |
Specifically, in this case, IE9 and IE8 detection.
Sometimes it is necessary to look for particular versions of a browser, IE being the most prevalent in my experience. This is a case where we look for IE8/IE9, then apply a class to the body, which makes it easy to apply a css style from that point.
In the header.php file, where the body tag begins, make sure it looks like this:
<body <?php body_class(); ?>>
In the functions.php file add the following:
add_filter('body_class','my_body_classes');
function my_body_classes($classes) {
$classes[] = 'default';
if (preg_match('/TRIDENT\/5.0/i', $_SERVER['HTTP_USER_AGENT'])) {
$classes[] = 'ie9-browser';
}
if (preg_match('/TRIDENT\/4.0/i', $_SERVER['HTTP_USER_AGENT'])) {
$classes[] = 'ie8-browser';
}
return $classes;
}
Using the HTTP_USER_AGENT user agent string, we can search it looking for TRIDENT/4.0 (for IE8) or TRIDENT/5.0 (which is found in IE9's user agent string). Once it's found, we can then alter the $classes[] array and add a class to identify the IE8 or IE9 browser.
Clearly, from there, easy to use css to target IE8 or IE9 only. This is another method to target IE8/9 if you don't want to use stylesheet hacks.