Tag Archives: wordpress

Plugin Review – Add Twitter RSS

It’s proving to be a great week for my plugin’s. Following on from WordCast PodCast mentioning WP Frame Breaker, Weblog Tools Collection have just posted a review of another one of my plugins – Add Twitter RSS.

If you use Add Twitter RSS, head on over and let them know what you think.

Add as always, feedback and requests for more options are always welcome.

WordPress 2.8 – New Widget API

WordPress 2.8 and the new Widget API

WordPress 2.8 is due sometime this month, and one of the major changes is the new Widget API.

Hand in hand comes the new Widget Panel which uses AJAX to update your sidebars. The latest beta has the new Widget Panel included:

WordPress 2.8 Widget Panel

WordPress 2.8 Widget Panel

Still very much a work in progress, the Panel doesn’t fully work yet and there’s a bit of work to be doen on the presentation side.

Multi Widgets

The biggest change is going to be for plugin writers. The new Widget API is designed to make the process of writing widgets, especially multi widgets easier.

As anyone who has ever written a multi-widget will tell you, the documentation is virtually non-existent and what tutorials are available are cryptic.1

The Widget API

As 2.8 will be released in the near future, now is the time to start thinking about updating your widgets to take advantage of the new API. As the Widget API isn’t fully documented yet, I decided to have a look at the source files to see what’s involved in writing a widget using the API.

The relevant files are:

  • /wp-includes/widgets.php
  • /wp-includes/default-widgets

Using the Widget API

The new Widget API provides a WP_Widget class and it’s by extending this class that you create your own widget. Your widget then over-rides 3 three methods within WP_Widget:

  1. widget()
  2. update()
  3. form()

The easiest way to demonstrate this is by creating a basic text widget. So we’ll build a WordPress 2.8 Text Widget.

Building Your Widget

As with the current version of WordPress, your widget needs to contain the some basic information so that WordPress will recognise it as a plugin:

<?php/*Plugin Name: New Widget APIPlugin URI: http://www.paulmc.org/whatithink/Description: Demonstration of the WordPress 2.8 Widget APIVersion: 1.0Author: Paul McCarthyAuthor URI: http://www.paulmc.org/whatithink*/

The first step is to create your widget class by extending WP_Widget:

class WP_My_Widget extends WP_Widget {

Your widget needs a constructor, that is, a function that will be called when the widget is loaded. Your constructor must have the same name as your widget. Within the constructor, specify an array to hold your widget options, control options and call the WP_Widget constructor.

function WP_My_Widget() {$widget_ops = array ('classname' => 'my_widget', 'description' => __('The description for your Widget') );$control_ops = array ('width' => '300', 'height' => '400');$this->WP_Widget('my_widget', __('My Widget'), $widget_ops, $control_ops);}

Once you’ve created your constructor, it’s time to over-ride the methods needed to make your widget work. The first method to over-ride is widget().

The widget() method is where your widget does it’s work. The widget() method takes two parameters. The first provides you with access to the widget display settings and the second provides access to that particular instance settings.

As this is a simple text widget, we’ll use just two settings – title and text.

function widget($args, $instance) {extract ($args);$title = empty($instance['title']) ? __('My Widget Title') : apply_filters('widget_title', $instance['title']);$text = apply_filters('widget_text', $instant['text']);echo $before_widget . $before_title . $title . $after_title;echo $text;echo $after_widget;}

The next method that we’ll over-ride is the update() method. This method provides us with the means to update and save our widget settings. It takes two parameters, the first is an array that contains the settings provided by the user via the widget form, and the second is an array containing the old settings for the widget.

function update ($new_instance, $old_instance) {$instance = $old_instance;$instance['title'] = strip_tags($new_instance['title']);$instance['text'] = $new_instance['text'];return $instance;}

That’s it. All the method does is store the old settings in an array, update the array with the new settings and then pass the new settings back to the WP_Widget class. The WP_Widget class handles the actual updating and saving.

Our next method to over-ride is form(). This method is used to display the widget control form. It takes one parameter, an array with the current settings.

function form ($instance) {$instance = wp_parse_args( (array) $instance, array('title' => '', 'text' => ''));$title = strip_tags($instance['title']);$text = format_to_edit($instance['text']);?><p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?><input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label><textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text ?></textarea></p><?php}}

That’s it. A simple text widget written using the WordPress 2.8 Widget API.

If you’d like to review the full code, with comments, then you can download it here:

New Widget API Plugin File

  1. I’ve been looking into transforming one of my widgets into a multi-widget, but given the hassle involved, I’m just going to hold off until 2.8 is released. []

New WordPress Plugin – Email Post Activation

I was looking into blogging via email1, and I couldn’t get it to work. Until I discovered that WordPress has to be told to check for any new emails.2.

In order to process the email post, WordPress uses wp-mail.php. Loading this page in your browser will tell WordPress to check for new email posts. There are three ways to call the file:

  • By visiting the URI to wp-mail.php in your browser,
  • By using Cron to automatically load the file after a set period of time, or
  • By including a hidden iframe in your Blog footer that links to wp-mail.php.

Not all WordPress users have the ability to edit their crontab and even more have no interest in editing their theme files. So I wrote a simple plugin that will add the iframe to the the blog footer.

When I say simple, I actually mean simple – there’s all of 3 lines of code in this plugin, so not much can go wrong with it.3

You can read more about it here.

  1. The reason for which I’ll go into in a later post []
  2. I should of realised this, but it was so obvious that I didn’t even consider it []
  3. Having just re-read that, it looks like I’m tempting fate. []

Using WordPress Templates to Create Forms

For the past week I’ve been working on integrating a custom form into a WordPress installation. Yes, I know there are a whole host of plug-ins out there that will allow you to add a form to your WordPress blog, but I had some pretty unique criteria that needed to be met. The criteria couldn’t be satisfied by a plug-in, so I had to roll my own.

The criteria were that the details returned by the form had to be stored in a separate database, had to have a form for user submitted data, a form for administration and had to allow me to add new queries as required. And all had to be accessible from within WordPress. So here are the basics of how I went about it.

The first step is to create a page template containing your form, e.g. my-form.php. Creating a page template is as easy as including the following lines at the top of your template:

<?php// Template Name: My Page Template?><?php require("my-form-functions.php"); ?><?php get_header(); ?><?php get_sidebar(); ?>

The last line of your template should be:

<?php get_footer(); ?>

Save the file in your theme directory. Use the following syntax when creating the HTML for your form:

<form name="my-form-name" action="" method="post">

There are two important points to note about the above code examples:

  1. The PHP require statement pointing to the name of the function file that will process your form, and
  2. the form action attribute is empty.

The first is important as I found that using your theme’s function.php can sometimes lead to problems. For example, I was unable to login to WordPress with my form processing contained in functions.php.

By leaving the action attribute empty, the form will be submitted to the containing page. Trying to direct the form to another custom page just results in a “Sorry, not found” error message from WordPress.

Because you are using the same page to display and process the form, you’ll need to create a PHP function that checks if the form is being submitted or if it is the users first visit to the page.

The easiest way to accomplish this is to include a hidden input in your form and to check the PHP $_POST global variable for the presence of the hidden input. For example, you could use code something like this:

function CheckSubmit() {if (array_key_exists('process-form', $_POST)) {.....do something.....}}

This code assumes that you have a hidden input named process-form. The value of the hidden input doesn’t matter as $_POST will only contain data if a form has been submitted. In other words, if the user has just accessed the page, $_POST will be empty and no code will be executed.

The last step is to create a new WordPress page and to use your new custom form page as the Page Template.

Due to the large number of people requesting a copy of the code I used, I’ve uploaded it my Page Template and you can download it here.

About my Code

File descriptions: comment-card.php is the page template that displays the form and com-functions.php contains the code that makes the form work.

Database details: This code was written to connect to a database containing 2 tables – responses and details. The com-functions.php file uses four constants to define your database connection details. These must be updated if you want your form to work.

Form questions: The questions in this page template are specific to the site I developed it for, so each question is validated accordingly. If you change the questions, you will also need to change the ValidateForm() function in com-functions.php.

Comment Admin: The download also contains an admin interface contained within the com-admin folder. This folder should be kept outside the WordPress hierarchy, in other words, save it to a completely separate folder from your blog.

The admin interface provides basic search and a form to send SMS text messages. The SMS facility is provided by a company called Esendex and the SMS form uses their PHP API. More information from the Esendex website.

If you have any questions, please feel free to post a comment.

New WordPress Plugin – Paul’s Latest Posts

I’ve written a new plugin for WordPress called Paul’s Latest Posts. This sidebar widget pulls your latest posts and displays them with an excerpt. I’ve just gotten confirmation from WordPress Plugins that it’s been approved for hosting on the official WordPress Plugins page. It’s not showing yet, but when it is I’ll provide the link and you can tell me what you think.

Updating Your WordPress Theme to Use More Than One Sidebar

Since I started working on my own themes, I’ve learnt a lot about how WordPress works. I’m still not at the level where I can write a theme from scratch, but I’m learning to modify themes for my own use. My first themes were based on either Public Domain or GPL themes, and I mainly stuck to modifying the CSS stylesheet.

My latest theme now uses five different sidebars to display information. There are four on the homepage (home.php) and one on the single post page (single.php). Most themes that I’ve come across implement one sidebar, or at most two. So how do you add more sidebars?

The first step is to edit your themes function.php file. Search the file for the following code:

if ( function_exists('register_sidebar') )

Some themes might also use the following code instead:

if ( !function_exists('register_sidebars') )

Within this section you’ll need to locate the code that tells WordPress how many sidebars you are using. Themes using a single sidebar will generally use this WordPress API function:

register_sidebar();

Themes using more than one sidebar, will use the following API function:

register_sidebars();

Both API functions take an array as a parameter. This array specifies the HTML code that appears before and after each widget that will appear in the sidebar. register_sidebars(); takes an additional parameter that allows you to specify how many sidebars should be used.

In either case, you’ll need to change this code to the following:

register_sidebars(x, array);

Replace x with the number of sidebars that you want in your theme. The array part of the code can be left as is.

The next step is to add the actual sidebars to the theme files that you want to contain the new sidebars. (Usually these would be either index.php, home.php, single.php, orarchives.php.)

Add the following code to the appropriate theme file:

<div class="sidebar_css_class"><ul class="sidebar_list_class"><?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(x) ) : ?><?php endif; ?></ul></div>

The class names used in this code example can be anything that you want, and will have to be added to your theme’s style.css file. Replace x with the specific number of the sidebar that you want to insert, e.g., if you want to insert a second sidebar into the theme, then your code will look like this:

<div class="sidebar_css_class"><ul class="sidebar_list_class"><?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(2) ) : ?><?php endif; ?></ul></div>

Upload the files to your theme directory, overwriting the original files if prompted. (To be extra safe, you should already have backed up the original files.) To configure the new sidebar, go to the Widgets sub-page under the Design page in your WordPress Admin and select your sidebar from the drop-down list.

Footnote: register_sidebar(); does allow more than one sidebar to be specified. It’s used in cases where each sidebar needs a different name and allows the theme designer to specify different layouts/ appearances for each sidebar. More information can be found on the relevant WordPress codex pages: register_sidebar(); and register_sidebars();.

Removing RSS Links in WordPress

The project I’m working on is a static site, so it doesn’t require a RSS feed. While I’m sure that it’s possible to remove the RSS functionality from WordPress completely, it’s probably a good idea not to. Messing around with the core functionality can do Bad Things.

A much easier way is to remove the links from your theme.

  • Using your preferred FTP program, browse to /wp-content/themes/theme_name/
  • Download header.php
  • Open the header.php file in your preferred text editor
  • Delete the following lines from the file:

<link rel=”alternate” type=”application/rss+xml” title=”RSS 2.0″ href=”<?php bloginfo(‘rss2_url’); ?>” />
<link rel=”alternate” type=”text/xml” title=”RSS .92″ href=”<?php bloginfo(‘rss_url’); ?>” />
<link rel=”alternate” type=”application/atom+xml” title=”Atom 0.3″ href=”<?php bloginfo(‘atom_url’); ?>” />

  • Save the changes and upload the new version of header.php into your theme directory.

Note 1: Not all three lines may be present in your theme.
Note 2: You can also comment out the RSS links using the HTML comment tags like this: <—— HTML Code ——>