Entries filed under 'Tips & Tricks'
You're reading all entries filed under 'Tips & Tricks'.
Tracking Outbound Links with Google Analytics Using jQuery
Here’s a quick event binding I wrote today for the site to track outbound links in Google Analytics using some jQuery selector trickery.
(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.
How to Exclude Pages from WordPress’ Search
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.
WordPress Development: Replacing Default Widgets
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.