Archive for the ‘Plugins’ Category.

Opera Tab Override User JS

A few days ago I released a WordPress plugin that overrides the default tab keypress action. This means when you press tab it inserts a tab character rather than moving focus from one field to another. I made a few modificaitons to this script in order to turn it into an Opera UserJS application. To use Tab Override for Opera, copy the following code and place it in a file called taboverride.js in your UserJS file:

function operaTab(e)
{
	if (e.keyCode == 9)
	{
		var obj = e.target;

		obj.selection = document.selection.createRange();
		obj.selection.text = String.fromCharCode(9);

		e.stopPropagation();
		e.preventDefault();
	}
	return false;
}

function tablistener(){
	var textareaArr = document.getElementsByTagName('textarea');
	for( var i in textareaArr )
	{
		textareaArr[i].addEventListener('keypress', operaTab, true);
	}
}
window.onload=tablistener;

Using UserJS
If you don’t have UserJS enabled or are unsure if it is enabled, here’s how you do it:

  • In Opera, on the toolbar click Tools -> Preferences...
  • Click the Advanced tab and select the Content submenu.
  • Make sure Javascript is enabled and click the JavaScript Options... button.
  • Under User JavaScript Files should be the path to your userjs file. The default is C:\Program Files\Opera\userjs.
  • If the path is not set, set it to the default. All you need to do to activate a User JS file is add it to the specified folder.

Some Issues with the script

The script will only work on textarea fields. This is intentional, as in other fields the default tab action is preferable. If you want to override all tabs, modify the line var textareaArr = document.getElementsByTagName('textarea'); to read var textareaArr = document.getElementsByTagName('form');

The script won’t work on some platforms due to interfering scripts. I’ve experienced this on some but not all vBulletin forums. I believe it is possible to override these scripts, but I have not yet figured out how.

WordPress Plugin: AdShare

I’ve just released my second plugin.

This one I created more out of necessity rather than interest. As I mentioned in a previous post I’m interested in finding tech writers to work on a group blog with me. The purpose isn’t to generate ad revenue, but it couldn’t hurt if we did. In the interest of fairly distributing any ad revenue, I wrote a plugin that displays ads based on the author of the current post. And that’s the story behind AdShare…

Give it a try and tell me what you think.

Plugin Compatibility

For various reasons, there are some people out that who have chosen to hang onto older versions of WordPress. Personally I do not recommend this behavior, but as a plugin developer, you might not want to exclude these people from using your plugin. Does this mean you should refrain from using features that were implemented in later versions for the benefit of those who have not upgraded? Absolutely not. One thing you can do, however, is to emulate or implement the newer features your plugin relies on for older versions.

This is actually very simple to do considering you can simply take the code directly from the latest version of WordPress. In order to do this properly, you should make use of PHP’s function_exists function. For example: the function admin_url was not part of WordPress until version 2.6. Normally, if you were to use this function, users with older version of WordPress would not be able to use it. You can, however, define it yourself:

if(!function_exists('admin_url'))
{
	function admin_url($path = '') {
	$url = site_url('wp-admin/', 'admin');

	if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
		$url .= ltrim($path, '/');

	return $url;
	}
}

Notice that the function declaration is inside the if(!function_exists(‘admin_url’)) block. If you simply defined the function as you normally would users with WordPress 2.6+ would receive an error.

Another thing to notice is that the code for admin_url contains a call to a function site_url. This function also was not defined until WordPress 2.6 so you’ll have to do the same thing for this function. For some functions it will be the case that you will have to do this for many functions, and in some cases you won’t be able to do this at all due to more significant differences between older and newer versions.

Here is a file I’m using called compat.php that defines several functions and constants that do not exist in earlier versions of WordPress.

//These functions replicate Wordpress functionality
//Used in cases where a Wordpress function is not
//available (ie a version that does not have that function)
if(!function_exists('is_ssl'))
{
	function is_ssl() {
		return ( isset($_SERVER['HTTPS']) && 'on' == strtolower($_SERVER['HTTPS']) ) ? true : false;
	}
}

if(!function_exists('force_ssl_admin'))
{
	function force_ssl_admin($force = '') {
		static $forced;

		if ( '' != $force ) {
			$old_forced = $forced;
			$forced = $force;
			return $old_forced;
		}

		return $forced;
	}
}

if(!function_exists('force_ssl_login'))
{
	function force_ssl_login($force = '') {
		static $forced;

		if ( '' != $force ) {
			$old_forced = $forced;
			$forced = $force;
			return $old_forced;
		}

		return $forced;
	}
}

if(!function_exists('site_url'))
{
	function site_url($path = '', $scheme = null) {
	// should the list of allowed schemes be maintained elsewhere?
	$orig_scheme = $scheme;
	if ( !in_array($scheme, array('http', 'https')) ) {
		if ( ('login_post' == $scheme) && ( force_ssl_login() || force_ssl_admin() ) )
			$scheme = 'https';
		elseif ( ('login' == $scheme) && ( force_ssl_admin() ) )
			$scheme = 'https';
		elseif ( ('admin' == $scheme) && force_ssl_admin() )
			$scheme = 'https';
		else
			$scheme = ( is_ssl() ? 'https' : 'http' );
	}

	$url = str_replace( 'http://', "{$scheme}://", get_option('siteurl') );

	if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
		$url .= '/' . ltrim($path, '/');

	return apply_filters('site_url', $url, $path, $orig_scheme);
	}
}

if(!function_exists('admin_url'))
{
	function admin_url($path = '') {
	$url = site_url('wp-admin/', 'admin');

	if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
		$url .= ltrim($path, '/');

	return $url;
	}
}

if ( !defined('WP_CONTENT_URL') )
	define( 'WP_CONTENT_URL', get_option('siteurl') . '/wp-content'); // full url - WP_CONTENT_DIR is defined further up

if ( !defined('WP_PLUGIN_URL') )
	define( 'WP_PLUGIN_URL', WP_CONTENT_URL . '/plugins' );

if(!function_exists('plugins_url'))
{
	function plugins_url($path = '')
	{
	$scheme = ( is_ssl() ? 'https' : 'http' );
	$url = WP_PLUGIN_URL;
	if ( 0 === strpos($url, 'http') ) {
		if ( is_ssl() )
			$url = str_replace( 'http://', "{$scheme}://", $url );
	}

	if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
		$url .= '/' . ltrim($path, '/');

	return $url;
	}
}

WordPress Plugins: Using Subversion

This article is meant to supplement WordPress’ Using Subversion Guide. Whenever I mention the guide I am referring to this.

So you’ve written a WordPress plugin and its now time to upload it to the WordPress plugin repository. Your first step will be requesting that your plugin be hosted on WordPress’ Plugin Directory. Sometime later your request will be accepted (provided it doesn’t drop all of the tables in the database or something like that) and you’ll be given access to the Subversion Repository.

If this is your first time making a WordPress plugin you might have no clue what subversion is, much less how to use it. WordPress provides some good Documentation on this subject, but I think there are a few things they left out that could be useful to someone using doing this for the first time.

Reading through WordPress’ Using Subversion guide will probably give you a pretty good idea of what subversion is, but by the time they get to actually using subversion you might be a little lost. I’ll pick up at the point I think it begins to get a little fuzzy:

Task 1: Starting from scratch with your brand new plugin repository

This section begins with a short overview of what you will be doing:

  1. Check out the blank repository.
  2. Add your files to the trunk/ directory of your local copy of the repository.
  3. Check in those files back to the central repository.

Then they toss a blob of text at you.

# Create a local directory on your machine to house
# a copy of the repository.

$ mkdir my-local-dir

# Check out the repository

$ svn co http://svn.wp-plugins.org/your-plugin-name my-local-dir
> A	my-local-dir/trunk
> A	my-local-dir/branches
> A	my-local-dir/tags
> Checked out revision 11325.

# As you can see, subversion has added ( "A" for "add" )
# all of the directories from the central repository to
# your local copy.

# Copy the plugin files to the local copy.
# Put everything in the trunk/ directory for now.

$ cd my-local-dir/
my-local-dir/$ cp ~/my-plugin.php trunk/my-plugin.php
my-local-dir/$ cp ~/readme.txt trunk/readme.txt

# Let subversion know you want to add those new files
# back into the central repository.

my-local-dir/$ svn add trunk/*
> A	trunk/my-plugin.php
> A	trunk/readme.txt

# Now check in the changes back to the central repository.
# Give a message to the check in.

my-local-dir/$ svn ci -m 'Adding first version of my plugin'
> Adding	trunk/my-plugin.php
> Adding	trunk/readme.txt
> Transmitting file data .
> Committed revision 11326.

# All done!

There are a few things you should know about the commands above. To begin, these commands should be entered on a unix shell. If you are familiar with unix you already know that text following the # symbol are comments and the text following the > symbol are the shell’s response to the commands. The commands themselves are not preceded by any symbol.

Another thing you should know is that the writers assume that you already have subversion (svn) installed. If you don’t like the idea of using a command line to access and modify the repository, there are a lot of GUI programs out there. You can find a listing of third party clients on the Subversion Website From this point on, however, I (and the writers of the guide I am referring to) will assume you have access to a unix command line with subversion installed.

If you are a windows user and don’t have access to a unix shell, I suggest using cygwin. SVN is part of the cygwin package, but you must explicitly specify that it be downloaded and installed. Read the cygwin documentation for more information. To test whether or not SVN is installed, type svn help in the shell.

Now that you have access to UNIX with SVN installed you should have no trouble running the commands in the guide.

Task 2: Editing a file that is already in the repository

Once you’ve uploaded your initial version, you will almost certainly need to make changes to those files sometime in the future. The guide is pretty clear on how to do this, so I will just provide a brief summary of the commands you will use for this.

svn stat shows you a list of files that differ from those in the repository.

svn ci -m "my message" updates the files in the repository with your local copies. It does not automatically add new files or remove old files. If you have added a new file that did not exist before you will have to use the svn add command to manually add this file. Likewise you will have to remove a file manually with the svn remove command. Documentation on any command can be obtained by using the svn help [command] command, where [command] is replaced by whatever command (ie add, remove, stat) you need documentation on.

Notice that the first command issued before updating any files is cd my-local-dir. It is very important that you are in you plugin directory when using any of these commands.

Task 3: “Tagging” a new version

Tagging refers to assigning a version to your plugin. Whenever you create a new version of your plugin you will need to tag that version. As the guide mentions, you do this by using the svn cp. It is important that you use the svn cp and not the normal cp command to do this. svn cp just doesn’t copy the files, it also adds the files as you would using the add command.

It is important to note that the command svn cp trunk tags/0.1 assumes that the folder 0.1 does not exist in the tags folder. You won’t get the desired result if it already exists.

As the guide mentions, it is important that the readme.txt file in your trunk folder is updated with the proper stable tag BEFORE copying the files.

Some Important Notes

Do not add, remove, move, or copy files with the normal commands (or through a GUI). To do these things you must use the svn [command] (ie svn add, svn move).

Stick to one application for managing your repository. It is not a good idea to use one program to add and remove files and then another to attempt to update the repository.

You can use whatever editor you want to modify your files. The example in the guide is nano trunk/my-plugin.php, but you don’t have to do everything from command line. Personally I use notepad++ to make changes to my files, just refrain from doing anything other than editing from your GUI or third party app.

Plugin Review: Hello World

This being my first plugin review, it seems fitting that it be a review of a plugin called Hello World. Lets start off with the basics:

Plugin: Hello World Code Generator
Author: Carl Sverre
Website: WordPress Plugin Directory, Home Page
Description: Generates a random hello world code snippet in one of many languages. Just drop the short tag [helloworldsnippet] into a post or page.
Compatibility: The plugin directory claims that it is downward compatible to WP 2.7, but I ran it on 2.5.1 without issue.

This is a plugin that really needs no description. The Hello World Code Generator, generates hello world code. This being the case I’ll get right into the four categories I’ll be judging the plugin on: Usefulness, Usability, User Interface, and Code.

Usefulness

Okay so generating hello world code for display in a post at random isn’t very useful, but this plugin does not claim to be useful. Rating this plugin on its usefulness wouldn’t be too fair when its goal is not to be useful. Considering that it does what it says it does I’m going to say that this category is not applicable.

Usability

This plugin requires no configuration and installation is a matter of dropping the files into your plugin directory. The use of the shortcode makes using this plugin exceedingly simple.

When I first saw this plugin the first thing that popped into my head was that it would be nice to drop it into my header, footer, or sidebar to display a short code snippet. This is not possible for two reasons, however. The first is that the this plugin uses short tags and they will only be parsed in posts or pages. The second is that some of these hello world code snippets consist of many lines; too many to insert into a header. In order to serve this purpose the code snippet probably shouldn’t be any more than three lines.

User Interface

Hello World has two output modes. If you use the [helloworldsnippet] tag it outputs the code without formatting. This is perfect if you intend to use it in combination with a syntax highlighter. If you use the [helloworldsnippet_pre] tag the code is wrapped in <pre> tags before outputting, which is perfect if you don’t have or don’t want to use a syntax highlighter.

Code

The code is pretty much what I would expect from a plugin this simple. Its clean and well written. One thing that I was happy to see was that this plugin cleans up after itself. On uninstallation the table it created in the database is dropped, which is exactly what you should expect when uninstalling a plugin.

To Summerize…

Hello World does exactly what it says it does more or less as well as anyone can expect. The only things I can think of to improve upon would be to use filters instead of (or in addition to) short tags and to have an output mode for only short code snippets.

Plugin: WP SimpleMail

I have an email account that I more or less, use exclusively for this blog. As such I thought it would be nice to incorporate that email account into my blog’s Dashboard. So I wrote a plugin that allows me to access an IMAP email account from my dashboard. You can download it Here.

Currently its only been tested on my blog. That being the case I would greatly appreciate some feedback and bug reports. Feel free to install it and give it a try.