Twitter like date format in WordPress

A tiny bit of code to make your dates more human readable by writing them the Twitter way; “3 days ago” or “7 hours ago”.

Lees verder >

Sorting the loop anyway you want to

Sort the posts in your website anyway you want to using a combination of the loop_start filter, the global $wp_query variable and the use of PHP’s usort function

Lees verder >

TinyMCE and headers

Most commonly your theme will have it’s headers (H1,Hh2, etc) set by the theme; you as a theme designer decide which header to use where. However, you want to give your client (the site’s editor) some control of how to use headers within the content of posts and pages. The TinyMCE Advanced plugin comes in handy here as it allows you to add a few extra buttons to the bar of the WordPress WYSIWYG editor. Unfortunately the dropdown that contains the options for paragraph, headers, and so on, also has the H1 right in there making it very easy for your editor to use it freely and thus making your website just that little bit less search engine friendly. I came across a tiny (pun intended) peace of code that gives just enough control to change that:

/*
* Modifying TinyMCE editor to remove unused items.
*/
function customformatTinyMCE($init) {
   // Add block format elements you want to show in dropdown
   $init['theme_advanced_blockformats'] = 'p,pre,h1,h2,h3,h4';
   $init['theme_advanced_disable'] = 'strikethrough,underline,forecolor,justifyfull';

   return $init;
}

// Modify Tiny_MCE init
add_filter('tiny_mce_before_init', 'customformatTinyMCE' );

Source: wordpress.org/support/topic/tinymce-formatting-options-remove-h1-h1-pre

By changing ‘theme_advanced_blockformats’ you can control exactly what shows up in that dropdown.

Prevent click event when scrolling on iPad

Here’s a few lines of simple javascript code to prevent click event inside of scrolling elements on an iOs device:

var iOs = agent.indexOf('iphone') >= 0 || agent.indexOf('ipad') >= 0; 
var clickEvent = iOs ? 'touchend' : 'click';
var touchMoving = false;
if (iOs)
{  
  document.ontouchmove = function(e)
  {
     touchMoving = true;
  }

  document.ontouchend = function(e)
  {
    touchMoving = false;
  }
} 

$(document).bind('ready', function() 
{
  $('#somelink').bind(clickEvent, function()
  {
    if (touchMoving) return false;
    // your code here
  });
}

Can’t find your way home?

Home is where your website starts but by default, Home doesn’t show up in your WordPress menu. Add this bit of code to your functions.php and it will magically appear:

function home_page_menu_args( $args ) 
{
  $args['show_home'] = true;
  return $args;
}
add_filter( 'wp_page_menu_args', 'home_page_menu_args' );

WordPress plugin of the day: Tweet

The Tweet plugin does two things; it rewrite Twitter usernames starting with @ and hashtags starting with # so that they automatically link to either the Twitter userpage or the Twitter searchpage. Very usefull for any modern blogger, but especially if you import your top tweets from your Twitter account into your blog … Judging by the ‘empty’ links above; it works a bit too well!

WordPress plugin of the day: Admin Menu Editor

Yesterdays plugin worked almost perfectly, but our editor was still seeing a few menu items he wasn’t supposed to. We used the Admin Menu Editor to change permissions needed for those menu items, thus making them disappear from the menu.

It’s typical for plugins to have their options under the Options or Extra menu items, but with a too low capabilities. We changed the edit_pages capability to manage_options so that only administrators could see them. The Admin Menu Editor plugin is also very handy for rearranging menu items and for renaming them.

WordPress Syndication and the_content filter

We use the FeedWordPress plugin to import Twitter updates into our blog, select the most interesting ones and put them on the homepage. However, the Tweet plugin wasn’t working. After a bit of testing we concluded that the_content filters were being run but totally ignored! As it turns out (after a lot of code digging) The FeedWordPress plugin actually rewrites the content of it’s imported posts to their original values thus bypassing any the_content filters. Add the following lines of code to your functions.php to prevent this:

remove_filter('the_content', 'feedwordpress_preserve_syndicated_content', -10000);
remove_filter('the_content', 'feedwordpress_restore_syndicated_content', 10000);

WordPress plugin of the day: Capability Manager

The great plugin Capability Manager allows you to change the default WordPress roles and create a few new ones as you see fit. We used it recently to allow an editor to create and modify users and remove the unused links section in the backend by removing the links capabilities.

Format any date according to WordPress date formatting settings

Format any date you like according to the date formatting settings in the WordPress backend with this simple piece of code. Put the following function in your theme’s functions.php file and enjoy the magic anywhere in your theme.

// $date is in yyyy-mm-dd format
function any_date($date = '2011-10-28')
{
  $format = get_option('date_format');
  return apply_filters('get_the_time', mysql2date($format, $date), $format); 
}

WordPress plugin of the day: Advanced Excerpt

WordPress’ post excerpt is by default nothing more than a shortened plain text version of your posts content and sometimes that can be too limiting. Enter Advanced Excerpt to give you full control over the excerpt; allow certain HTML tags, adjust the length by word or character count, add your own read-more text, etc.

Hierarchical WordPress templates

Another useful piece of code for you to put into your functions.php file. You probably already know that a file in your theme dir called category-1.php will be used for showing content for the category that has an id of 1. The filters below allow you to have hierarchical templates based on category id. For instance, all single posts belonging to category 1 will use single-cat-1.php. Also, all child categories of category 1 will use category-1.php.

Lees verder >

WordPress plugin of the day: p2pConverter

Created a few dozen pages that in hindsight should have been posts? It even happens to the best (that’s us) and fortunately there’s a handy WordPress plugin out there that can fix this for you: p2pConverter.

AddThis sharing + Google Analytics event tracking

We love AddThis. By adding a tiny bit of code to a website, visitors can easily share what they like with the rest of the world. AddThis also tracks what gets shared. All of our websites have Google Analytics embedded, so wouldn’t it be great if AddThis sharing statistics would appear in Google Analytics? That only requires three lines of code! How? Read it in the AddThis Help section.

WordPress plugin of the day: Regenerate Thumbnails

During the development of a website you might need to change the sizes of your thumbnails to match designs change, unfortunately your thumbnails won’t automatically resize and that’s when this plugin comes in handy. With one click of a button (and a cup of coffee later) your thumbnails will be … regenerated.

WordPress plugin of the day: Resize at Upload Plus

This plugin does exactly what it says: it will resize images at upload. Works great when uploaders have a tendency to upload multimegapixel images directly from their camera 😉 We prefer a maximum size of 1280 pixels.

Download it here; wordpress.org/extend/plugins/resize-at-upload-plus/

Multiple permalinks for a single post with multiple categories in WordPress

Ever had more than one category assigned to a post? Did you get stuck with a single permalink where you might have wanted a dynamic permalink based on the current category? Read on and find out how to ‘hack’ your WordPress theme to make this happen.

Lees verder >

WordPress thumbnail quality

While searching for a way to change the default jpeg quality of generated thumbnails in WordPress, I came along this simple piece of code:

function jpeg_quality_filter($quality) { return 100; }
add_filter('jpeg_quality', 'jpeg_quality_filter');

Add to the functions.php file in your theme directory and change ‘100’ to the desired jpeg compression value (higher is better).