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.

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 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);

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); 
}

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 >

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).