WordPress Plugin Development

«»

SHARE & COMMENT :

Checking the plugin

You can go ahead and activate the plugin. The plugin will be activated but will do nothing at
this moment.

Time for Action – Testing the version check

Let’s just make sure that the version check works, by requiring a fictional version of
WordPress that does not exist yet:

  1. Deactivate the plugin and change the version check code to a higher version. For
    example, replace 2.6 with 5.0.
  2. 
    	if (version_compare($wp_version,"5.0","<"))
    

  3. Re-upload the plugin and try to activate it again. You will see a WordPress error and
    a message from the plugin:

What just happened?

The version check fails and the plugin exits with our predefined error message. The same
thing will happen to a user trying to use your plugin with outdated WordPress installation,
requiring them to update to a newer version.

Have a go Hero

We created a basic plugin that you can now customize.

  • Change the plugin description to include HTML formatting (add bold or links to
    the description).
  • Test your plugin to see what happens if you have two plugins with the same name
    (upload a copy of the file under a diff erent name).

Displaying a Digg button

Now it's time to expand our plugin with concrete functionality and add a Digg link to every
post on our blog.

In order to create a link we will need to extract post's permalink URL, title, and description.
Luckily, WordPress provides us with a variety of ways to do this.

Time for Action – Implement a Digg link

Let's create a function to display a Digg submit link using information from the post.

Then we will implement this function into our theme, to show the link just after the
post content.

  1. Add a function to our plugin to display a Digg link:
  2. 
    	/* Show a Digg This link */
    	function WPDiggThis_Link()
    	{
    		global $post;
    		// get the URL to the post
    		$link=urlencode(get_permalink($post->ID));
    		// get the post title
    		$title=urlencode($post->post_title);
    		// get first 350 characters of post and strip it off
    		// HTML tags
    		$text=urlencode(substr(strip_tags($post->post_content),
    								0, 350));
    		// create a Digg link and return it
    		return '<a href="http://digg.com/submit?url='.$link.'&
    			title='.$title.'&bodytext='.$text.'">Digg This</a>';
    		}
    

  3. Open your theme's single.php file and add a call to our function just below the
    line with the_content(). If you are not sure how to do this, see the forthcoming
    section on "Editing the theme files".
  4. 
    	<?php if (function_exists(WPDiggThis_Link)) echo WPDiggThis_
    								Link(); ?>
    

  5. With the default WordPress theme, this change will look something like this (you can
    also refer to the following image):
  6. After you save the theme file, your blog posts will now automatically have the Digg
    This
    link shown after the content:
  7. Clicking the link will take the user directly to the Digg site, with all the required
    information already filled in:

Well done! You have created your first working WordPress plugin!

What just happened?

When WordPress loads a post, the single.php template file from the currently active
WordPress theme is ran. We added a line to this file that calls our plugin function
WPDiggThis_Link() just after the content of the post is displayed:


	<?php the_content('<p class="serif">Read the rest of this entry
	»</p>'); ?>
	<?php if (function_exists(WPDiggThis_Link)) echo WPDiggThis_
	Link(); ?>

We use function_exists()to check our function because it exists only if our plugin is
installed and activated. PHP will generate an error if we try to run a nonexistent function. But
if we deactivate the plugin later, we don't want to cause errors with our theme. So, we make
sure that the function exists before we att empt to run it.

Assuming that the plugin is present and activated, the WPDiggThis_Link() function from
our plugin is ran. The first part of the following function gets information about our post and
assigns it to variables:


	/* Show a Digg This link */
	function WPDiggThis_Link()
	{
		global $post;
		// get the URL to the post
		$link=urlencode(get_permalink($post->ID));
		// get the post title
		$title=urlencode($post->post_title);
		// get first 350 characters of post and strip it off HTML tags
		$text=urlencode(substr(strip_tags($post->post_content),
					0, 350));

We use the urlencode() PHP function for all the parameters that we will pass to the final
link. This will ensure that all the values are formatt ed properly.

The second part uses this information to construct a Digg submit link:


	// create a Digg link and return it
	return '<a href="http://digg.com/submit?url='.$link.'&
		title='.$amp;title.'&bodytext='.$text.'">Digg This</a>';
	}

It returns this HTML text so that it gets added to the WordPress output at the point where
the function is called – just after the post is displayed. Therefore, the link appears right after
each post—which is convenient for the user who has just finished reading the post.

«»

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

*

Close
Please support the site
By clicking any of these buttons you help our site to get better