jump to navigation

Weekly Tip 2: Using Snoopy to Fetch Content March 11, 2006

Posted by weeklytips in Uncategorized.
8 comments

Sorry for being a day late on this week’s tip, I got caught up in life last night. This week’s tip is a quick, but extremely useful tip. Often times plugins need to fetch some content over the web, or send a request back to a server to report some info. However, often times for security purposes, webhosts have disabled url wrappers for functions like fopen, leaving you without an easy way to send your request.

WordPress avoids this problem with a handy class called Snoopy, which uses sockets to make a request to remote servers and avoid the problem. Here’s a quick rundown on using Snoopy for some simple tasks.

/* First, we need to make an instance of the class */
$snoopy = new Snoopy();
/* Now we can fetch a url from the web */
$result = $snoopy->fetch(‘https://weeklytips.wordpress.com’);
/* Check to make sure that it was fetched successfully*/
if($result) {
/* Now the variable $snoopy->results contains the information you just fetched, for now we will just echo it out */
echo $snoopy->results;
} else {
echo ‘We were not able to complete your request’;
}

The power of Snoopy goes way beyond simple fetching of URLs, but for now this is all I will cover here. There are tons of good resources and tutorials for Snoopy on the web, so if you need to learn more, you can check those out for now. At some later date, I may also publish the portion of the book on some of Snoopy’s more advanced features, and how they relate with the rest of WordPress.

Advertisements

Extra Tip: Object Oriented Plugin Development March 5, 2006

Posted by weeklytips in Uncategorized.
3 comments

Note:As this was slightly chopped together from the book itself, it has some references to an earlier example project provided by the book. Do not fret, you should be able to figure out what’s happening, the example is pretty easy (this assumes you know the basics of writing a procedural plugin first).

To avoid function collision and pollution, many people have wanted to resort to building plugins with classes and methods instead of prefixed functions for their plugins. WordPress has the capability out of the box to support these sorts of plugins, but unfortunately the documentation around them is lacking.

Adapting Procedural Plugins

To show the differences between procedural and object oriented plugins, we’re going to first adapt the simple plugin from our first example to show the basic differences. If you don’t already know how to write in PHP style object oriented syntax, it may be in your best interest to read a bit of the php.net documentation (4, 5) on the subject first. Our original procedural plugin for profanity filtering looked like this:

function prefix_filter_profanity($content = ”) {
$profane_things = array(‘water’, ‘cold’, ‘fast’, ‘cheese’);
$clean_things = array(‘wine’, ‘hot’, ‘slow’, ‘pasta’);
$filtered_text = str_replace($profane_things, $clean_things, $unfiltered_text);
return $filtered_text;
}
add_filter(‘the_content’, ‘prefix_filter_profanity’);
add_filter(‘comment_text’, ‘prefix_filter_profanity’);

Adapting the plugin is incredibly simple.

class Profanity_Filter() {
/* This is for PHP5, which uses a new magical function as the constructor */
function __construct() {
$this->Profanity_Filter();
}
function Profanity_Filter() {
add_filter(‘the_content’, array(&$this, ‘filter’));
add_filter(‘comment_text’, array(&$this, ‘filter’));
}
function filter() {
$profane_things = array(‘water’, ‘cold’, ‘fast’, ‘cheese’);
$clean_things = array(‘wine’, ‘hot’, ‘slow’, ‘pasta’);
$filtered_text = str_replace($profane_things, $clean_things, $unfiltered_text);
return $filtered_text;
}
}
$profanity_filter = new Profanity_Filter;

The first look at this class may be a little overwhelming. The code has gotten considerably longer, but this is an exception rather than the rule, as longer plugins will often have their size cut down.

Let’s step through bit by bit.

function __construct() {
$this->Profanity_Filter();
}
function Profanity_Filter() {
add_filter(‘the_content’, array(&$this, ‘filter’));
add_filter(‘comment_text’, array(&$this, ‘filter’));
}

The first function, __construct, is for PHP5 and future versions. It basically ensures that the old style constructor, the method that matches the class’s name, is run when the class is created. The function Profanity_Filter is the old style constructor for PHP4. This function registers the actions for filtering on the content and comment text. This is similar to the old code, except the second argument, the callback function. Instead of passing in a function name as a string, you pass in an array, which corresponds to the PHP callback type. The callback type tells PHP how to find the function or method that its looking for. The first item in the array is a reference to $this, which is a reserved variable pointing to the current instance of the class. The second argument is the string name of the method inside the function that should be run as the filter, just like before.

This code can also be written in a second way, if your goal is solely to avoid name collisions.

class Profanity_Filter() {
function Profanity_Filter() {
add_filter(‘the_content’, array(&$this, ‘filter’));
add_filter(‘comment_text’, array(&$this, ‘filter’));
}
function filter() {
$profane_things = array(‘water’, ‘cold’, ‘fast’, ‘cheese’);
$clean_things = array(‘wine’, ‘hot’, ‘slow’, ‘pasta’);
$filtered_text = str_replace($profane_things, $clean_things, $unfiltered_text);
return $filtered_text;
}
}
/* ClassName::Method and array(‘ClassName’, ‘Method’) are both valid static callback types */
add_filter(‘the_content’, ‘Profanity_Filter::filter’);
add_filter(‘comment_text’, array(‘Profanity_Filter’, ‘filter’));

This method uses static calling of methods instead of requiring that you create an instance of a class. The best idea is to choose which method you use based on what your plugin needs to do. If you have code that would normally be written as object oriented code with multiple objects, the former method is the way for you, while the latter example is just for avoiding function name collisions.

Extra Tip: Removing Admin Menus March 4, 2006

Posted by weeklytips in Uncategorized.
10 comments

While writing various plugins, I’ve come across the need (or want more likely) to remove some entries from the WordPress admin menus. While there isn’t an immediate API hook for this, there is still a quick bit of code to allow you to remove the items. Let’s say for example that we would like to remove the Comments menu under Manage. If you look in /wp-admin/menu.php, you’ll find something like the following (unimportant bits stripped out of course):

$menu[10] = array(__(‘Manage’), ‘edit_posts’, ‘edit.php’);

$submenu[‘edit.php’][20] = array(__(‘Comments’), ‘edit_posts’, ‘edit-comments.php’);

Each item in the $menu array represents a top-level menu item, while each item in the $submenu array represents one of the submenu pages. So how can we remove the menus? unset() of course.

function prefix_remove_menu() {
global $submenu;
unset($submenu[‘edit.php’][20]);
}
add_action(‘admin_header’, ‘prefix_remove_menu’);

If you really wanted, you could change that to something like this, and remove top menu items instead.

function prefix_remove_menu() {
global $menu;
unset($menu[10]);
}
add_action(‘admin_header’, ‘prefix_remove_menu’);

Weekly Tip 1: Peacefully Degrading Template Tags March 3, 2006

Posted by weeklytips in Uncategorized.
3 comments

Welcome to the first (well, so called weekly) WordPress tips post. This blog is dedicated to giving out tips to WordPress plugin developers and possibly even theme authors on little tricks you may or may not have known were possible with WordPress. Keep your eyes peeled every Friday for a guaranteed new tip, and occasionally days inbetween for some extras. And now stand by for this week’s tip.

A problem that many plugin developers face is having plugins that provide template tags that will gracefully fail when their plugin is removed. A majority of current plugins simply have a function that you call, similar to all of the other template tags, to place the output in your themes. The problem is that if a user uninstalls the plugin, they are now faced with a fatal PHP error in their theme because your function can no longer be found. (more…)