Category: WordPress

Plugin developer, Core contributor and support volunteer. Yeah, I’m a WordPress fan!

  • Using Google Analytics data to show popular posts

    Using Google Analytics data to show popular posts

    Google Analytics Dashboard is an excellent plugin for showing site analytics on your WordPress dashboard.

    However, it also has an open API built-in allowing anybody to access statistics from their own code. As a result I’ve created a new of small functions for my own site. To get these to work, ensure you have Google Analytics Dashboard installed, active and you’ve authenticated yourself using the OAuth method!

    The most useful is the one I use in the sidebar to display the most popular posts. I was using specific plugins which track visits independently. However, I found these to inconsistent and unreliable.

    This isn’t the version I use, as I’ve had to make specific modifications to only display posts and to tidy up the page titles. However, this is the same base code just with my very specific changes removed.

    function latest_posts_list( $days, $showposts, $cache_hours ) {
    
       $cache_key = 'ga_posts_' . $days . $showposts;
      $output = get_transient( $cache_key );
    
      if ( !$output ) {
    
         $output = '';
    
        if ( $days == 0 ) {
           $start = '2006-09-01';
         } else {
             $start = date( 'Y-m-d', ( time() - ( 60 * 60 * 24 * $days ) ) );
         }
        $end = date( 'Y-m-d' );
        $thispost = 1;
    
         $login = new GADWidgetData( get_option( 'gad_auth_token' ), get_option( 'gad_account_id' ) );
        $ga = new GALib( 'oauth', NULL, $login -> oauth_token, $login -> oauth_secret, $login -> account_id );
          $pages = $ga -> pages_for_date_period( $start, $end );
    
        foreach( $pages as $page ) {
           $url = $page[ 'value' ];
             $title = $page[ 'children' ][ 'value' ];
             $output .= '<li><a href="' . $url . '" rel="nofollow">' . $title . '</a&gtl';
            if ( current_user_can( 'edit_posts' ) ) { $output .= ' (' . $page[ 'children' ][ 'children' ][ 'ga:pageviews' ] . ' views)'; }
            $output .= '</li>';
            $thispost ++;
           if ( $thispost > $showposts ) break;
         }
        set_transient( $cache_key, $output, 3600 * $cache_hours );
     }
     echo $output;
    }

    Add this code to functions.php and then edit the line $start = '2006-09-01'; to reflect the date on which your blog statistics started. Then call latest_posts_list  with 3 parameters, all of which are required…

    1. The number of days across which to gather statistics. If you specify 0 then it will be for all time.
    2. The number of posts to display in the list.
    3. The number of hours to cache the results.

    A sidebar example, with checks for function availability, would be…

    <?php if ( function_exists( 'latest_posts_list' ) ) : ?>
        <li>
            <h2>Recent Popular Posts</h2>
            <ul>
                <?php latest_posts_list( 30, 5, 24 ); ?>
            </ul>
        </li>
    <?php endif; ?>

    This will display the 5 most popular posts within the last 30 days and will update this list every 24 hours.

    Two further functions that I use elsewhere add live statistics data to a posts content. The code can also be added to your functions.php file…

    function ga_visits_shortcode( $paras = '', $content = '' ) {
    
     extract( shortcode_atts( array( 'days' => '' ), $paras ) );
      if ( $days == '') { $days = 30; }
       $start_date = date( 'Y-m-d', ( time() - ( 60 * 60 * 24 * $days ) ) );
       $end_date = date( 'Y-m-d' );
    
     $login = new GADWidgetData( get_option( 'gad_auth_token' ), get_option('gad_account_id') );
       $ga = new GALib( 'oauth', NULL, $login -> oauth_token, $login -> oauth_secret, $login -> account_id );
       $visit_data = $ga -> total_visits_for_date_period( $start_date, $end_date );
     return number_format( $visit_data[ 'value' ] );
    }
    add_shortcode( 'ga_visits', 'ga_visits_shortcode' );
    
    function ga_pageviews_shortcode( $paras = '', $content = '' ) {
    
     extract( shortcode_atts( array( 'days' =>'' ), $paras ) );
       if ( $days == '') { $days = 30; }
       $start_date = date( 'Y-m-d', ( time() - ( 60 * 60 * 24 * $days ) ) );
       $end_date = date( 'Y-m-d' );
    
     $login = new GADWidgetData( get_option( 'gad_auth_token' ), get_option( 'gad_account_id' ) );
     $ga = new GALib( 'oauth', NULL, $login -> oauth_token, $login -> oauth_secret, $login -> account_id );
       $visit_data = $ga -> total_pageviews_for_date_period( $start_date, $end_date );
     return number_format( $visit_data[ 'value' ] );
    }
    add_shortcode( 'ga_pageviews', 'ga_pageviews_shortcode' );

    Now, all you have to do is call either shortcodes – [ga_visits] or [ga_pageviews] to output the number of visitors of pageviews that your site has had. Useful for promoting your site. There is one parameter, days, which allows you specify the time range this is for. If you don’t specify this parameter then 30 is assumed.

  • WordPress 3.1.2 released

    WordPress 3.1.2 has been released.

    This release addresses a vulnerability that allowed Contributor-level users to improperly publish posts.

    The issue was discovered by a member of our security team.

    We suggest you update to 3.1.2 promptly, especially if you allow users to register as contributors or if you have untrusted users. This release also fixes a few bugs that missed the boat for version 3.1.1.

    The official WordPress announcement

    3.1.2 Codex Details

    Change Details

    A list of the modified files (between WordPress 3.1.1 and 3.1.2)

    Download 3.1.2 (entire installation)

    Download 3.1.2 (just the changed files between 3.1.1 and 3.1.2)

  • Damn you, Professional WordPress Plugin Development

    So, I’m finally beginning to get somewhere with version 2 of my YouTube Embed WordPress plugin. As a birthday treat to myself I bought Professional WordPress Plugin Development from Amazon.

    Now, completion of my plugin is even less likely to be soon as this rather excellent book has just left me with a raft of further changes to make. Everything from coding standards to widget development is covered. I’m only a quarter way through and already there are sticky note bookmarks everywhere – each indicating something I need to look at.

    It’s not just this plugin this will affect though, as I will then turn my intention towards all my other plugins as well – starting with my caching and feed plugins which will get major overhauls (as this book mentions methods of performing each with a lot less effort than I’m currently applying!).

    Even their section on marketing your plugin has left me with ideas of changes I wish to make – first up, I’m going to see about renaming my existing plugins.

    If you’re into WordPress development I can’t recommend this book highly enough.

    Now, if you’ll excuse me… I have a lot of work to do.

  • Plains laid for WordPress 3.2

    WordPress have given some initial details of the plans for WordPress 3.2.

    Rather than adding lots of new features, this release is going to concentrate on speed improvements and restricting the WordPress minimum requirements (this will allow existing code to be removed and will make adding code in future easier).

    The requirements changing are…

    • PHP 5.2.4 required – WordPress will simply be dropping support for PHP 4 (i.e. there won’t be very many new PHP 5 features added)
    • MySQL 5 Required – like above, WordPress will simply be dropping support for MySQL 4
    • Internet Explorer 6 – no more fancy IE6-only hacks. WordPress will be officially discontinuing support for IE6 and instead providing a “use a real browser” nag screen (something this site already has installed)

    With support for long-outdated technologies being dropped, they promise this will be the fastest and lightest WordPress in quite some time. To this end they will also be focussing on speed improvements all around, including (but certainly not limited to) the Dashboard and admin menu.

    Two further announced changes are…

    • New Fullscreen Editor –  a new fullscreen editor that’s “more beautiful, more useful, and simpler.”
    • Better Upgrades – only changed files will be upgraded in future. Yay!

    WordPress have also promised faster release cycles in future (concentrating on what’s promised and not adding further changes later on).

    Certainly, I’m excited by this 😀

  • Do WordPress users need Jetpack?

    Automattic, the people behind WordPress have announced JetPack. In essence, it’s a plugin for self-hosted WordPress owners that adds functionality that WordPress.com bloggers have had for a while.

    That sounds a good thing, right?

    Well, most of the plugins (bar one) are available separately from Automattic and, this way, you can at least only install the components you actually need (reducing bulk and load on your installation).

    Here’s a run down of what Jetpack adds and where else, if possible, you can get the same functionality from…

    • WordPress.com stats – site stats delivered from WordPress.com. This is no longer available as a separate plugin.
    • Twitter Widget – the plugin library is chock full of Twitter widgets and general plugins. However, Automattic have their own Twitter widget named Wickett Twitter Widget which, looking at the code, looks pretty much to be the same plugin!
    • Gravatar Hovercards -various blogs have posted about how to add these to your WordPress blog yourself (as I have done). Alternatively, download a plugin from the directory.
    • WP.me shortlinks – a way of adding the WP.me URL shortener to your blog. As far as I can tell, this is not available as a separate plugin.
    • Sharedaddy – a social sharing tool. There are various alternatives, or you can download Sharedaddy from the plugin directory.
    • LaTeX – let’s you mark up your posts with the LaTeX markup language which is perfect, for example, for complex mathematical equations. Download from the plugin directory.
    • After the Deadline – adds advanced spell check and grammar checking facilities. Download from the plugin directory.
    • Shortcode Embed – puts LOADs of useful shortcodes to your fingertips, including video embedding. However, I suspect there are few of these that can’t be found elsewhere in other plugins. Instead of bulking up your site with shortcodes that may not be used, why not just install plugins to add those that you need?

    From what I can tell, the only feature that you can’t get elsewhere is “WP.me shortcuts” (a feature I’d like!).

    So, the answer is to install those that you want separately? It would be, but there’s a dark cloud in the Jetpack FAQ…

    As we upgrade each of our individual plugins to be a part of Jetpack, we’ll prompt you to switch over to the new, Jetpack-powered version.

    Oh. You’re going to be forced to install all of them as Jetpack.

    I don’t get it. What’s the point of Jetpack? Ok, so they’ve bundled a number of (their) plugins together – but isn’t the joy of WordPress plugins that you can mix and match as you choose from different authors, getting each just the way you need it?

    This post has been updated from the original, as I thought that individual components of Jetpack couldn’t be deactivated, which would have lead to more serious concerns. After further testing, however, I’ve found that this is not the case.