Here’s a quick event binding I wrote today for the site to track outbound links in Google Analytics using some jQuery selector trickery.
Update
I’ve been using this a lot, and, rather than copying and pasting it repeatedly, I decided to develop it into a real and true jQuery plugin. Download it and fork away!
(function ($) {
$(function () {
$('a[href^="http:"]:not(a[href^="http://' + document.location.hostname + '"])').click(function () {
try { _gat._getTrackerByName()._trackEvent('Outbound Links', $(this).attr('href')); } catch (e) {}
});
});
}) (jQuery);
What’s going on here?
The selector’s doing most of the work here - looking for anchors linking to absolute URLs, and then basically filtering out anchors with absolute URLs pointing to the current document’s domain. When the outgoing links are clicked, we fire off the event tracking method in Google Analytics. This clicks will be categorized as “Outbound Links” and the anchor’s full URL will be tracked as an Action.
I’ve been working on a site recently that’s needed a lot of pseudo sprites, and I was getting frustrated with creating sprite maps by hand, so I created simple Photoshop plugin that takes a directory of images and merges them into a CSS friendly sprite map.
Download the Plugin
I’ve added the plugin to GitHub, so download now and fork away!
While working on the new theme for the site, I quickly realized that I didn’t want to show pages in any of the searches on the site. I only wanted posts.
It was easy enough to find articles that illustrated ways to exclude certain categories or posts by ID, so I decided to apply those methods to pages as well. Simply paste the following into your theme’s functions.php file.
function gdmExcludePagesInSearch($query) {
if ($query->is_search) {
$query->query_vars['post__not_in'] = get_all_page_ids();
}
return $query;
}
add_filter('pre_get_posts', 'gdmExcludePagesInSearch');
This will remove all pages from the loop, and only on the search page.
Something unclear, or have something to add? Please leave a comment.
I’ve been working on a little WordPress project these last few weeks with a colleague. When I first heard the idea, I thought, “That should be easy enough to build.” I decided the best way to tackle the feature would be a custom widget, but when I finally sat down to get it working I quickly began to realize that there were a few obstacles to overcome.
The project was really meant to extend an existing widget, and it seemed confusing to have both the default and extended version there side by side (plus it would be really nice to call them the same thing for transparency). I needed to get rid of it. You can’t just overwrite an existing widget by registering a new one by the same name - WordPress ignores it. And if I did manage to find a way to remove a default widget, when do you have to do it?
Luckily, some digging around found a solution.
function gdm_widget_meta_register() {
// unregister the widget and its control
wp_unregister_sidebar_widget('meta');
// register the new and improved widget and control
wp_register_sidebar_widget('meta', __('Meta'), 'gdm_widget_meta');
wp_register_widget_control('meta', __('Meta'), 'gdm_widget_meta_control');
}
add_action('widgets_init', 'gdm_widget_meta_register', 1);
The really important pieces are the wp_unregister_sidebar_widget function and the widgets_init action hook. widgets_init actions are run right after all the default widgets are registered, giving us the perfect opportunity to call wp_unregister_sidebar_widget with the ID of the widget you’re getting rid of. Now you can register your own widget and control, effectively replacing a default WordPress widget.
I received an email this evening from Dan, who uses the Page Link Manager, wondering how to get a home page link to show up in his site navigation. I had never really thought about the problem, or even realized it was a problem until I started my search for a solution. There’s not much out there covering the issue besides a few forum posts at WordPress.org. But it’s a problem, nevertheless. I understand that the blog’s heading is supposed to link to your home page, but I believe there’s a large audience of Internet users, and potential readers, that wouldn’t think to look to a heading for a shortcut back to your home page.
And so we have the Home Page Link plugin.
The Plugin
The plugin does just what you think - adds a Home link to your site navigation (wp_list_pages). And it’s very easy to use.
- Download and unzip the plugin archive
- Place the plugin file under wp-content/plugins directory on your Wordpress Installation
- Log in to your admin interface and activate Home Page Link under the ‘Plugins’ tab
That’s it! You should now be able to view your site with its shiny new Home link.
Like my other plugins, I hope to keep this going as a work in progress, and work along with the WordPress community to make it more useful and efficient. If you have something you’d like to see in an upcoming version, please don’t hesitate to add a comment or contact me.
Requirements
The current release requires a server running at least PHP4. The plugin has been tested on Wordpress 2.x. If anyone has gotten it working on older versions of Wordpress, please let me know.
Resources
If you’re interested in writing plugins, the WordPress plugin tutorial is an excellent resource.