Category: Development

  • Copying to clipboard in WordPress

    I was recently asked, as a commercial request, to create a WordPress plugin that would allow users to click a button and copy text the clipboard. It seemed an easy enough thing to do – 2 weeks later, though, and I’ve thrown in the towel.

    Using JavaScript to capture contents is the easy bit – adding it to the clipboard is a lot more difficult. IE has a JavaScript command built in, other browsers vary. Unfortunately, other browsers (e.g. Firefox) also restrict this ability for security reasons. This is because the ability to write to the clipboard also comes with the ability to read from it as well – quite why this functionality can’t be detached and restrict just the reading I don’t know.

    So, using JavaScript is problematic.

    Thankfully I’m not the first to come across this problem and a third party script named ZeroClipboard is available. This uses Flash to update the clipboard, which doesn’t have the same security limitations. Of course if you’re viewing from a device that doesn’t support Flash (cough, splutter, iPad, iPhone, cough) then you’re still out of luck. None-the-less this seemed a perfect solution.

    Unfortunately, my limited JavaScript skills failed me – I found the program to be over-complex for what I needed and failed to be able to get it to work.

    At this point I found an alternative – ZClip uses ZeroClipboard but is controlled via JQuery (which is built into WordPress) and provide a much easier and friendly method of access.

    I was now in a position where I had a working plugin. Until I used Admin Bar. That seemed to affect the positioning of the Flash overlay so you had to click slightly above the “Copy to clipboard” button to get it to work.

    The customer, though, didn’t have an issue with this and I could only hope that they didn’t have anything else within their theme or plugins that could affect it in this way (I did try contacting the developer of Zclip but got no response).

    What really ended it all though was the limited way of capturing text in zclip, which I hadn’t appreciated earlier on. ZClip can capture in 2 ways – from static fields (e.g. the text between a SPAN) or dynamic (e.g. from a field). The first doesn’t capture any formatting, even paragraph breaks – returning everything as one long line of text. The second did, but by capturing the HTML.

    The customer wanted the customer to be able to copy to the clipboard long paragraphs of text – neither offered a neat solution to this.

    Then I gave up. One day I may return to it – certainly ZeroClipboard will probably allow me to do what I need it to, but I’ll have to get my head around how to first.

  • Making money from WordPress

    If you’ve come to this post thinking I’m sharing some amazing strategies on making money from WordPress, you’ll probably be disappointed.

    WordPress, and a result themes and plugins produced for it, are licensed under the GPL. What this means is that although you can product commercials versions, people are free to use and copy your results. Additionally, WordPress doesn’t integrate well with, for example, commercial plugins as there is no place for them on their site and, hence, no automatic updates.

    Paid support is another option for plugin and theme authors but even they don’t seem to very popular, with one company who provides such support recently shutting down.

    I’ve had a few requests to make changes to or write new plugins, but they usually don’t pan out particularly well – with so many plugins already available there’s usually a reason why one doesn’t exist 😉

    I get money from the advertising on this site but that barely covers the spiralling hosting costs. There are also various promotional methods further, but these are more to do with having a website rather than WordPress in particular.

    So, getting rich is probably best left to the entrepreneur and those coming up with “the next big thing”.  But that’s fine with me – WordPress is GPL for a reason and it’s all about sharing. If I was in it for the money I’d have done something else instead!

  • 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.