Open links in a new window with jQuery
It is possible to open links in a new window by adding target="_blank" to the anchor tag in HTML, but if your page uses the XHTML doctype it will generate a validation error. The following snippet uses JavaScript instead to open the link in a new window, and keeps your XHTML valid.
Make sure you have jQuery loaded, then include this in your JavaScript file:
|
1 2 3 4 5 |
$('a[rel="external"]') .click( function() { window.open( $(this).attr('href') ); return false; }); |
Now you just need to add rel="external" to any anchor tags that you want to open in a new window (e.g. <a href="somedomain.com" rel="external">some link text</a>).
(Update: Apparently using target="_blank" is valid in HTML5. But this JavaScript method will still work too).
Custom WordPress widget PHP code snippet
Add this to your functions.php file:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
// Register widgets if (function_exists('register_sidebar')) { register_sidebar(array( 'name' => __('Widget Name'), // Change this to your own name 'id' => 'widget-id', // Change this to your own id 'description' => __( 'This is a custom widget.' ), // Change this to your own description 'before_widget' => '<div id="%1$s" class="%2$s">', 'after_widget' => '</div>', 'before_title' => '<h3>', 'after_title' => '</h3>' )); } |
Then add this the theme file(s) where you want the Widget to appear:
|
1 2 3 4 5 6 7 |
<?php /* Widgetized area. */ if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Widget Name') ) : ?> // Match 'Widget Name' with the name of the registered Widget you are using. // Some text to display when no widgets have been added. <h3>Widget Area!</h3> <p>This is a Widgetized section.</p> <p>If you can see this you need to go to <strong>Appearance > Widgets</strong> in your WordPress admin and add some widgets.</p> <?php endif; ?> |
Now when you go the Widget manager in your WordPress Admin you can add some widgets.
Track PDF downloads with Google Analytics
Use this code to track PDF downloads (or other non-HTML files) with Google Analytics. Be sure to change myfile to your actual filename, both in the url and the tracking code.
|
1 |
<a href="http://www.mydomain.com/myfile.pdf" onClick="javascript: _gaq.push(['_trackPageview', '/downloads/myfile']);"> |
Now when the visitor clicks the link to download the file, Google Analytics will record it as a pageview. By prefixing each file name with /downloads/, you can then easily identify and filter these pageviews in your Google Analytics reports.