Quick FieldsA WordPress PluginBy |
I recently created a new blog on my readersclubs.com site where I write reviews of movies and television shows I see. I enjoy watching and I have a lot of opinions. I also did it as a means of helping myself learn more about using WordPress, which is the software that runs my blogs.In particular, I wanted to write a plugin, for the sake of practice. A film criticism site seemed a good place to do that. So, for your not-so-entertaining enjoyment, here is the info on a WordPress plugin I wrote. Now, don’t go asking why I haven’t offered it to the WordPress community. I’ll tell you why before you ask. It isn’t ready yet, and I’m not sure I’m going to get it ready. There are other plugins that do similar things. If I want to really polish it up, I will. For now, I did it as a test of my skills and to have something to write about here.
The film review blog is called, “Any Screen Will Do.” Since a blog made up of film reviews is likely to have a lot of the same information in it, such as who the directors, producers, and actors were, it made sense to me to have them be fields I could fill in instead of narrative I had to write each time I did a review.
Look at any review on the blog and you will see at the bottom of it, a list of these items. I did not write a new table for each review. I just created fields in the dashboard to fill in whenever I write a review. The plugin then formats them to appear at the bottom of the review.
Here’s the table from the review of a movie called, “Dark Night of the Scarecrow:”
Director(s): | Frank De Felitta |
Producer(s): | J.D. Feigelson |
Featured Cast Members: | Charles Durning Larry Drake Jocelyn Brando Lane Smith |
Release Year: | 1981 |
Writer(s): | J.D. Feigelson |
To create a plugin for WordPress, you create a directory for it under the plugins directory on your server. IN that new directory, you write the code for the plugin. Comments in the code tell WordPress the name of the plugin.
[note color=”#bbffff”]/*
Plugin Name: Quick Fields
Plugin URI: http://www.innervessel.com/quickfields/
Description: Allows entry of quick fields such as director, producer, year for film and television. Facilitates themed control of placement.
Author: Rex Goode
Version: 1.0
Author URI: http://www.rexgoode.com/
*/
[/note]
I won’t be giving the entire code for you. The purpose of writing this is not to give you a plugin to use, but to describe some of the steps of writing one.
You won’t necessarily find my way of doing it exactly in the WordPress documentation. Most of the WordPress documentation tells about writing plugins with standard programming techniques. I, on the other hand, always prefer to work with objects whenever I can. (For more on object-oriented programming, see OOP’s! Did I do that?!) So, my Quick Fields plugin was written as a class called “quickfields”.
[note color=”#ccffff”]class quickfields {
var $dbVersion = “1.0”;
var $all_fields;
var $table_name;
var $postId;
…
}
[/note]Almost everything that happens in the plugin is in the confines of the quickfields object that is created at run time. In fact, the only things that happen outside of the object are the WordPress housekeeping items at the start of the file and the following lines at the bottom, which are explained in the comments.
[note color=”#ccffff”] if (class_exists(“quickfields”)) { $qf = new quickfields(); } // Create a object of type quickfields//Actions and Filters
if (isset($qf)) {
// Tell it what to do when the plugin is activated
register_activation_hook(__FILE__,array(&$qf, ‘quick_fields_activate’));
// Tell it what to do when the plugin is deactivated
register_deactivation_hook(__FILE__,array(&$qf, ‘quick_fields_deactivate’));
// Show the table of values
function the_quick_fields() {
global $qf, $post;
$qf->quick_fields_display($post->ID);
}
}
[/note]The “register_activation_hook” and “register_deactivation_hook” functions are the way a programmer tells WordPress what to do when someone activates or deactivates a plugin, respectively. What I told them to do are to run member functions of my quickfields class, namely, “quick_fields_activate” and “quick_fields_deactivate”
The first one creates a database table to hold the names and information about the quick fields that the blog author can create. The second one destroys the table.
I also create a function called, “quick_fields_display”, which goes into the database, grabs the values entered by the blog author, and displays them on the post. Where it displays them depends on where the theme calls the quick_fields_display function. My Clive theme that I created (more on that some day maybe) calls this function after the body of the post is displayed.
Now, at about this point, I bet I’ve lost most people who read my blog. I won’t mind if you give up right now. This is as much for my future reference as it is for others who might want to write WordPress plugins.
So, some of you Object-Oriented purists are wondering why I didn’t make quick_fields_display a member function of the quickfields class. I could have. Right now, the code in the theme looks like this:
[note color=”ccffff”]<?php the_quick_fields(); ?>
[/note]If it would have been a member function, it would have looked like:
[note color=”ccffff”]<?php if (isset($qf)) { $qf->the_quick_fields(); } ?>
[/note]In some ways, the latter is probably not a bad idea. I will think about it.
At this point, my code for creating and destroying the table is working. Next, I had to give the blog owner a way to decide which quick fields he wanted to create.
If you are familiar with the WordPress Dashboard, you know that there is a Settings menu. I chose to put my Quick Fields functionality under that menu. I accomplished that with the following member function:
[note color=”#ccffff”]function quick_fields_menu() {
add_options_page(‘Quick Fields Settings’, ‘Quick Fields’, ‘manage_options’, ‘qfoptions’, array($this, ‘quick_fields_options’));
}
[/note]This points the new Settings menu item to another member function, “quick_fields_options”, for control of the Quick Fields settings.
The quick_fields_options function and other related functions allow the owner of the blog to decide which quick fields to make available to authors. Take a look at how it looks.
In future versions, I might create a way to change the order they appear. For now, it is first-in/first-out.
So, up to this point, the functionality exists for a blog owner to go in and create a series of special fields that the author of a blog post might want to add to his post. The functionality is not yet in place for the author of the post to fill in those fields. That is the last piece.
It is accomplished through something that WordPress calls, meta boxes. A meta box is something that shows beneath the main box where you write when you’re writing a blog post. If you are using the WordPress excerpt field, this is a meta box. Each field that is defined by the blog owner in the process above gets its own meta box. In my next version, I will make all of the fields appear in a single meta box.
Here is the code for the current version:
[note color=”#ccffff”]foreach($this->all_fields as $field) {
$slug = “qf” . $field->fieldSlug;
$fieldkey = “qf_” . $field->fieldSlug;
$fieldargs = array(“fieldInstructions” => $field->fieldInstructions, “keyname” => $fieldkey);
add_meta_box($slug, $field->fieldLabel, array(&$this, ‘quick_field_post’), ‘post’, ‘normal’, ‘high’, $fieldargs);
}
[/note]
It looks like this:
The values a reviewer types into these fields shows up at the end of his review in tabular form as shown above. He doesn’t have to remember to write them into his narrative somewhere. He just fills in the form and the plugin does the rest.
Now, if you’re still following me, you might be wondering where I store the information the reviewer types into these fields. I didn’t need to create a database table for it. Each WordPress database has a table called “wp_postmeta” or for networked sites like mine, “wp_n_postmeta” where “n” is the blog ID number. For information on how to do this, look for the documentation for the get_post_meta, update_post_meta, and add_post_meta WordPress functions.
I enjoyed writing a WordPress plugin. It employed several of my skills, including:
- Object-oriented PHP language design and coding
- MySql database administration and design
- Javascript form processing
- HTML coding and design
When I make some of the improvements I have in mind, I might consider posting it at WordPress.com for others to try. If you find yourself trying to make a WordPress plugin and are having trouble, respond here and I’ll try to help. It’ll make a good learning exercise for me. Good luck.
$slug = “qf” . $field->fieldSlug;
$fieldkey = “qf_” . $field->fieldSlug;
$fieldargs = array(“fieldInstructions” => $field->fieldInstructions, “keyname” => $fieldkey);
add_meta_box($slug, $field->fieldLabel, array(&$this, ‘quick_field_post’), ‘post’, ‘normal’, ‘high’, $fieldargs);
}