Our Blog

Make Shortcodes Work in all WordPress Post Excerpts

Need your shortcodes to render when displaying post excerpts? This snippet does just that, regardless of whether the excerpt is manually set or automatically generated.

First, we need to unregister a WordPress core filter and register our own slightly modified version.

Add the following lines of code to your plugin file or functions.php file.

remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'custom_trim_excerpt');

Next we copy and paste the function “wp_trim_excerpt” from “wp-includes/formatting.php” into our file and rename it to “custom_trim_excerpt”.

Now just comment out the line that calls “strip_shortcodes” so it looks like the code below.

function custom_trim_excerpt($text = '')
{
	$raw_excerpt = $text;
	if ( '' == $text ) {
		$text = get_the_content('');
 
		//$text = strip_shortcodes( $text );
 
		$text = apply_filters('the_content', $text);
		$text = str_replace(']]>', ']]>', $text);
		$excerpt_length = apply_filters('excerpt_length', 55);
		$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
		$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
	}
	return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}

The last step is to make sure all the excerpts (manual and automatic) are run through the “do_shortcode” function. Add the line below and you are good to go!

add_filter('get_the_excerpt','do_shortcode');

Adding a second blog page to your WordPress site

I recently received a request from a client who wanted to have their WordPress loop visible on the home page as well as on a “blog” page. The posts that would be displayed on the home page would be slightly different from those on the blog page.

The desired effect can be achieved using the following process.

First, duplicate the your default loop template file, usually index.php and rename it to blog-page.php

At the top of this file, insert this comment so that any static page can use this template file by setting the template drop down by editing the page in the WordPress dashboard.

<?php
 
 	/*  	Template Name: Blog Page    */
 
?>

Next set up an alternate post query in the template file by creating a new instance of WP_Query. This is a better approach than calling query_posts(), since query_posts() alters the global query variables, which will have some undesirable results( e.g. your “blog” page won’t get the current-menu-item class applied to it in the navigation, so the link item can’t be highlighted).

Use the code below to create a new instance of WP_Query. Pass in the following default query vars so that post paging works correctly. It might also be helpful to run your alternate query variables through a filter, so you can hook into this query in a plugin or in the functions.php file and further customize it.

<?php 
 
global $paged; 
$alternate_blog_query_vars = apply_filters(
 	'alternate_blog_query_vars',
 	array(
 		'posts_per_page'=>get_option('posts_per_page'),
		'paged'=>$paged
	)
);
 
$alternate_query = new WP_Query($alternate_blog_query_vars);
 
?>

Next, alter the loop functions to use the $alternate_query instead of the main query. Look for the calls to have_posts(), and the_post(), and change them so it looks like this:

<?php 
 
if ($alternate_query->have_posts()) :
 
	while ($alternate_query->have_posts()) : $alternate_query->the_post();
 
		<!-- loop code goes in here -->
 
	endwhile;
endif;
 
?>

That should just about do it. You can now set any page in your WordPress website to display the posts that would normally appear only only the home page. You can also use the filter ‘alternate_blog_query_vars’ to modify the posts that are shown on these pages.

The only problem I ran into with this approach is that the wp-paginate plugin wouldn’t recognize that this post needed to be paginated, because it only considers the main loop when it is rendering its pagination buttons. I fixed this by temporarily making my custom query the main query, then resetting it once the pagination was rendered so that nothing would break further down in the code.

<?php
 
  if(function_exists('wp_paginate')) {
 	global $wp_query;
  	$tmp_query = $wp_query; 
	$wp_query = $alternate_query;	
	wp_paginate();
	$wp_query = $tmp_query;
 }  
 
?>

Easy as pie!

Make My Blog Honest Tutorial Plugin

Hey all! Thanks for attending my presentation. Here are some additional resources for you:

How to add the current menu item CSS class to a custom link in a WordPress menu

So the new WordPress menus introduced in 3.0 are great but the Menus panel of the WordPress Dashboard limits us in what we can add to our menus. If, for example, we want to add archive pages for custom post types we end up having to add them as “Custom Links”. This is an easy work-around, but doing this robs us of the ability to highlight the current menu item when a user is viewing this page.

Here is a simple hack that you can add to your themes functions.php file, or build into your plug-in in order to identify when a “Custom Link” page is active, and add the “current-menu-item” class.

add_filter('nav_menu_css_class', 'AddCurrentMenuItemClass',1,2);
 
function AddCurrentMenuItemClass($classes,$item)
{
$link = site_url().$_SERVER['REQUEST_URI'];
if(strpos($link, $item->url) !== false)
{
$classes[] = 'current-menu-item';
}
return $classes;
}

If you don’t want the menu item to get the current-menu-item class when viewing a sub-page, use the following:

add_filter('nav_menu_css_class', 'AddCurrentMenuItemClass',1,2);
 
function AddCurrentMenuItemClass($classes,$item)
{
$link = site_url().$_SERVER['REQUEST_URI'];
if(strcmp($link, $item->url) == 0)
{
$classes[] = 'current-menu-item';
}
return $classes;
}

Make sure the url for the Custom Link matches the format of the $link variable. That is to say, it should be the full site_url (you can find out this value by looking at Settings > General in the WordPress Dashboard) followed by your archive page slug.

See us speak at Word11 in Toronto

Come check out the Word11 24 hour blogging festival going down in Toronto on August 27th where I’ll be giving a talk on intermediate WordPress plugin development.

From now until August 24th you can register and use the code SPEAKERS11 for a 50% discount. Learn more at http://word11.com

flipCounter, our contest countdown jQuery plugin

Groupon - Time left to buyOn our most recent project, we held an online giveaway where the first 100 entrants would win a free bag of coffee. In designing the signup form, I really wanted to present the user with a nice visual counter that would show the user exactly how many samples were still up for grabs. Nothing spurs users into action more then a deal that’s going to expire, and nothing gives users more confidence than knowing that they aren’t the only ones that have signed up. The success of this philosophy can clearly be seen on sites like Groupon. So I set off building a nice countdown graphic drawing inspiration from my cars odometer, analogue clocks and retro webpage hit-counters.

Once I had my graphic designed, I thought I’d invoke a little jQuery to give it some pizzaz. I banged out the script in a few minutes and it was good to go. We launched the site, hit Facebook, Twitter and the Aurora Importing blog and waited for the traffic to roll in.

It took some time for us to get our first sign ups. In fact on the first day, the form sat completely untouched. If we were to run this contest again, I think I would avoid starting off the contest at an even number. When you see “100 samples available”, people tend to assume the counter is simply non functional, rather than believe they are actually the first person to sign up. It’s also scary being the first person through the door. What if you get ripped off? What if you’re the only one that falls for it?

On day two John did some digging and found some interesting forums, twitter accounts and deal websites to mention the contest too. Not long after I started seeing the counter tick down to zero, a within about an hour and a half, it was all over!

I had a look at my jQuery code, and seeing that rest of the week wasn’t looking to busy, I set off inspired to write my first legit jQuery plugin. I ‘plugged in’ (to borrow a term from “The Social Network”), and before I knew it, it was all done!

If you’ve got a minute, check out flipCounter, BloggingSquared’s first jQuery plugin, and let us know what you think!

flipCounter - a jQuery counter plugin