WordPress Plugin Development

Using the Digg API

Usually, when using the functionalities of third-party sites, as we are doing in our example
with Digg, we would search for the API documentation first. Almost all the major sites have
extensive documentation available to help developers use their services in an eff ective way.

Digg is no exception, and if you search the Internet for the digg butt on api you will find a
page at http://digg.com/tools/integrate that will have all the details we need in
order to implement our Digg functionality.

Digg allows us to use several diff erent ways of using their service.

For the start, we will display just a Digg link. Later, we will expand it and also display a
normal butt on.

Here is what the Digg documentation says about formatting a submit link.

Submit URL:

http://digg.com/submit?url=example.com&title=TITLE&bodytext=DESCRIPTI
ON&media=MEDIA&topic=TOPIC

Submit URL Details:

  • url=example.com

    Maximum length is 255 characters

    Story URL should be unique and devoid of session or user-specific data

    Please URL-encode all strings as appropriate. For example:

    http%3A%2F%2Fyourwebsite%2Fyourstoryurl%2Fstorypagedetails.html
  • title=TITLE

    Maximum length is 75 characters

    Please also URL-encode the story title
  • bodytext=DESCRIPTION

    Maximum length is 350 characters

    Please also URL-encode the body text

Using this information, we are able to create a valid link for the Digg service from the
information available in our post.

Acquiring post information

WordPress provides a number of ways to get information about the current post.

One of them involves using the global variable $post, which stores all the relevant
information for the current post. We have used it in our example to extract the post title and
content, but it can also be used to get other information such as post category, status and
so on.

WordPress also off ers an array of functions we could have used to access post information
such as get_the_title() and get_the_content().

The main diff erence between using these functions and accessing post data directly
using $post variable is in the end information we get. The $post variable contains raw
information about the post, just as the user wrote it. The functions mentioned above take
the same raw information as a starting point, but could have the final output modified by
external factors such as other active plugins.


	You can browse through the wp-includes/post-template.php file of
	your WordPress installation to get a bett er understanding of the diff erences
	between using the $post variable and the WordPress provided functions.

Post permalink URL

In order to obtain post URL we used the get_permalink() WordPress function. This
function accepts the post ID as a parameter, and as a result, returns post’s actual URL on the
blog. It will always return a valid URL to your post no matt er what permalink structure your
blog is using.

Editing the theme files

In our example, we had to edit our theme in order to place the Digg link under the post
content. WordPress allows for easy theme editing through the built-in Theme Editor panel.

After selecting the theme you want to edit, you will be presented with a number of
options. Every theme consists of various PHP template files, each covering diff erent
blog functionalities.

Here is a reference table detailing the most commonly used template files.


Always be careful when editing the theme files as any kind of mistake in your syntax can
cause an error in displaying the page. It is therefore good practice to first backup theme files,
so you can safely revert to them afterwards.


	Quick reference
	$post: A global WordPress variable containing information about the currently
	processed post.
	get_permalink($post_id) : Returns the full URL to the post given by its
	ID (for example $post->ID).
	function_exists($function): Helps the PHP function to check if
	the given function exists. It is useful in themes when we want to include
	our function.
	urlencode($string): Helps the PHP function to properly format the
	parameters to be used in a URL query.

Have a go Hero

Our plugin already has useful functionality. Try to customize it by:

  • Calling our Digg link function from diff erent places in the theme template, for
    example, before the content or after the tags are displayed (look for the_tags()
    line in the template).
  • Adding the function to other theme templates such as the main index file and
    archive pages to display the Digg links on the home page and blog archives as well.
  • Using the get_the_title() and get_the_content() functions to obtain post
    title and content instead of using the $post variable.

Comments

comments

Pages: 1 2 3 4 5

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*