Adding a Digg button using JavaScript code

Our Digg link works fine for submitting the content, but isn't very pretty, and does not show the number of Diggs we received. That is why we need to use a standard Digg button.

This is accomplished by using a simple piece of JavaScript code provided by Digg, and passing it the necessary information.

Time for Action – Implement a Digg button

Let us implement a Digg button, using information from the Digg API. We will use the newly created button on single posts, and keep the simple Digg link for all the other pages.

  1. Create a new function for displaying a nice Digg button using JavaScript code.
    /* Return a Digg button */
    function WPDiggThis_Button()
    {
        global $post;
                    
        // get the URL to the post
        $link=js_escape(get_permalink($post->ID));
                    
        // get the post title
        $title=js_escape($post->post_title);
        
        // get the content    
        $text=js_escape(substr(strip_tags($post->post_content), 0, 350));
                    
        // create a Digg button and return it    
           $button="
           <script type='text/javascript'>
           digg_url = '$link';
           digg_title = '$title';
           digg_bodytext = '$text';
           </script>
        <script src='http://digg.com/tools/diggthis.js' type='text/javascript'></script>"
                    
        return ($button);    
    }
  2. Modify our filter function to include the Digg button for single posts and pages, and a Digg link for all the other pages:
    /* Add Digg This to the post */
    function WPDiggThis_ContentFilter($content)
    {        
    // if on single post or page display the button    
    if (is_single() || is_page())
            return WPDiggThis_Button().$content;        
        else
            return $content.WPDiggThis_Link();        
    }
    
  3. Digg button now shows at the beginning of the single post page.
    Time for Action – Implement a Digg button

What just happened?

WordPress will parse our content filter function according to the conditional statement we have added:

function WPDiggThis_ContentFilter($content)
{        
// if on single post or page display the button    
if (is_single() || is_page())    
    return WPDiggThis_Button().$content;    

This means that if the current viewed page is a single post or page, we will append our Digg button at the beginning of that post.

If we are viewing all the other pages on the blog (like for example the home page or archives) we will show the Digg This link instead.

    if (is_single() || is_page())
        return WPDiggThis_Button().$content;        
 else
 return $content.WPDiggThis_Link();
 
}

The reason for doing so is that we do not want to clutter the home page of the blog with a lot of big yellow Digg buttons. So we just place a subtle link below the post instead. On single pages, we show the normal button using our new WPDiggThis_Button() function.

The first part is similar to our previous WPDiggThis_Link() function, and it acquires the necessary post information.

/* Return a Digg button */
function WPDiggThis_Button()
{
    global $post;
    
    // get the URL to the post
    $link=js_escape(get_permalink($post->ID));
    
    // get the post title
    $title=js_escape($post->post_title);
    
    // get the content    
    $text=js_escape(substr(strip_tags($post->post_content), 0, 350));    

However in this case, we are treating all the information through the js_escape() WordPress function, which handles formatting of content for usage in JavaScript code. This includes handling of quotes, double quotes and line endings, and is necessary to make sure that our JavaScript code will work properly.

We then create a code using Digg API documentation for a JavaScript button:

// create a Digg button and return it
    $button="
    <script type='text/javascript'>
    digg_url = '$link';
    digg_title = '$title';
    digg_bodytext = '$text';
    </script>
    <script src='http://digg.com/tools/diggthis.js' type='text/javascript'></script>";

Conditional Tags

We have used two functions in our example, is_single() and is_page(). These are WordPress conditional tags and are useful for determining the currently viewed page on the blog. We used them to determine if we want to display a button or just a link.

WordPress provides a number of conditional tags that can be used to control execution of your code depending on what the user is currently viewing.

Here is the reference table for some of the most popular conditional tags.

Conditional tags are used in a variety of ways. For example, is_single('15') checks whether the current page is a single post with ID 15. You can also check by title. is_page('About') checks if we are on the page with the title 'About'.

Note

Quick reference

is_single(), is_page(): These are conditional tags to determine the nature of the currently viewed content

js_escape(): A WordPress function to properly escape the strings to be used in JavaScript code

WordPress Conditional Tags: http://codex.wordpress.org/Conditional_Tags