Sermon Posts 0.11

Added a couple more options to adjust the size of the Thickbox “Add/View Files” pop-up, hopefully I’ll make it dynamic at some point.

Speaking of which: The “Add/View Files” button now works! It’s even pretty close to what I wanted! Check it out, try and break it, then let me know what happened because I’m pretty sure there are still bugs. It took me forever, wow.

Which reminds me: This plugin is still BETA! I just want to make it clear, since it still has the possibility to be insecure, or trash your data. I try hard to do the Right Thing, but it’s getting to be pretty big.

Other SVN-esque notes: Piles of new functions added to sermonposts.php, and pretty much the whole fileupload.php was rewritten. The metabox.php really doesn’t have much different, just the link to the thickbox pop-up changed.

I removed a bit of code that was supposed to allow for plugin updating for unofficial plugins. It would have been nice to have during beta, but I couldn’t get it working and was planning on taking it out anyway, so now it’s gone. Wheeee!

Sermon Posts 0.09

Added functionality to be able to add new Series/Service/Preacher, although not on the “Add Sermon” page (future update). The HTML displays on the same page, clicking the “Add New” button changes it’s visibility state. Thanks to Joe for the catch :-)

The import procedure has been given a once-over, so if your Series/Services/Preachers were importing incorrectly, you can try again now.

Repaired a date issue in the “Add Sermon” metabox that was saving months one greater than they should have been.

The settings window now has a few settings, it will display the rest as soon as they are added to the database (some options are currently hard-coded).

Additionally, there is now a link at the bottom of the “Settings > Sermon Posts” page that leads to a database cleaner. If your import broke, you can now go there and remove all sermons, sermon files (hopefully!), and taxonomy items associated with this plugin. You’ll need to disable and re-enable the plugin to recreate a couple tables.

School is getting increasingly busy, with finals coming up in only three weeks, so I won’t be as active as my initial burst. If you have suggestions, now is a perfect time to make them!

WordPress Tutorial: Creating a new pop-up

Have you ever wondered how to make your own pop-up I-Frame in WordPress, like the one used in blogs? Something that looks like this, but with custom interior HTML?

You can probably think of about a billion uses for something like this, but there are probably very few uses that I would consider legitimate. Using the approach I outline below gives great power, but with it, as Uncle Ben says, comes great responsibility.

I’ll be outlining the code used, and describing each section carefully, but you can download the final code from this link (a zip file, installable directly as a plugin).

There are two files here, the first one is the core code for the plugin, and the second is what creates the pages within the pop-up page. Here’s what’s in the first file:

Core Plugin File

Most plugins that I write are enclosed inside a PHP class, to avoid namespacing issues. PHP now supports proper namespaces, but may not be supported on older version of PHP. For this example, we’ll create the basic class:

1
2
$MyThing = new MyThing_Class;
class MyThing_Class {

Now, you don’t need to put the following information as variables, but for the purpose of this tutorial I think it will be helpful:

1
2
3
4
5
6
private $html_name = 'myupload';
private $page_name = 'mynewpage';
private $title_name = 'My Sweet Pop-Up';
private $popup_width = '600';
private $popup_height = '400';
private $link;
  • $html_name is the name used by the pop-up link, and is part of the add_action used to add the pop-up.
  • $page_name is part of the URL as $_GET['paged']
  • $title_name is the title of the pop-up box
  • $popup_width and $popup_height are the size parameters for the pop-up box
  • $link holds the link to the thickbox page

Inside PHP classes, the function __construct() is run whenever the class is initialized with $var = new MyClass so the function is used to place all the add_action and similar hooks:

1
2
3
4
5
6
7
function __construct()
{
add_action( 'admin_menu', array( $this, 'MyMenu' ) );
add_action( 'media_upload_'.$this->html_name, array( $this, 'MyFrameAdder' ) );
add_action( 'admin_init', array( $this, 'AdminInit' ) );
$this->link = get_bloginfo('url')."/wp-admin/media-upload.php?tab={$this->html_name}&TB_iframe=1&width={$this->popup_width}&height={$this->popup_height}";
}

The add_action sets should be familiar, the second paramater is the function name, but inside a class it is stored as an array, with $this being a reserved PHP variable, and the second element of the array as the function name.

Let’s look at the functions listed:

1
2
3
4
5
6
7
8
9
10
function MyMenu()
{
add_options_page(
	'My Thing Options',
	'My Thing',
	'manage_options',
	$this->page_name,
	array( $this, 'MyDisplay' )
);
}

The function MyMenu is called when WordPress creates the admin menus. In this example we’ll just add a menu item in “Settings”. The first two lines are the two labels used, the second one, My Thing in this case, is the label that will show up in the “Settings” menu. The third element, manage_options is a test that determines which users will be able to see the menu item. See the Roles and Capabilities section for more tests.

1
2
3
4
5
6
7
8
function AdminInit()
{
if ( isset( $_GET['page'] ) && $_GET['page'] == $this->page_name )
{
wp_enqueue_script( 'thickbox' );
wp_enqueue_style( 'thickbox' );
}
}

It’s bad practice to just queue up scripts everywhere, so we limit it to just the settings page made in the MyMenu function.

1
2
3
4
function MyDisplay()
{
echo "<p><a id='my_thickbox' class='button-secondary thickbox' title='{$this->title_name}' href='{$this->link}'>Click Here!</a></p>";
}

The function MyDisplay would normally display settings, and you would want to work out a way to show and save them, but here all we are doing is adding a link which will call the Thickbox pop-up when clicked.

1
2
3
4
function MyFrameAdder()
{
	wp_iframe( array( $this, 'MyUploadForm' ) );
}

We need to use wp_iframe (defined in /wp-admin/includes/media.php) to register the frame used to display our new pop-up HTML. The Thickbox pop-up is actually just an embedded iframe, styled to look like a pop-up.

1
2
3
4
function MyUploadForm()
{
	include( 'popuppage.php' );
}

All this does is call in the pop-up display PHP. Depending on how big your setup is, you might find it just as handy to print out the HTML right here, of course.

All that’s remaining for this file is to end the class with a }

The Pop-Up File

The pop-up file is the file included in the previous MyUploadForm function. It basically just spits out HTML, but you’ll want at least one security check, this is the start of the file:

1
2
3
4
< ?php
if ( !current_user_can('upload_files') ) :
wp_die(__('You do not have permission to upload files.'));
else:

We can just wrap the whole file in this if statement, to secure it a little more. Now the rest of the output is just whatever HTML you want. Here’s a sample that shows how a simple two-paged pop-up could be constructed:

1
2
3
4
5
6
7
8
if ( isset( $_GET['paged'] ) && $_GET['paged'] == '2' )
{
echo '<p>This is page 2.</p>';
}
else
{
echo "<p><a href='{$this->link}&paged=2'>Click to go to page 2.</a></p>";
}

That's really it! Here's what it looks like right now:

Final Notes

The variable $_GET['page'] is reserved by WordPress, and if you use it in your pop-up you'll end up with a broken page, usually it dies saying "You don't have sufficient permission" or similar.

Obviously you'll need to use some wp_nonce security checks inside your pop-up window.

For media management, check out the file at /wp-admin/media-upload.php for a little inspiration.

I don't think I ever would have figured this out without the help of jameslafferty, so a big thanks to him!

Again, here's the full code download.

Sermon Posts 0.08 – Importing Fixed

Finally! The import procedure should be fully functional now! If it finishes and says “there were errors”, but doesn’t throw any PHP errors (red), it just means there were some verse ranges from the Sermon Browser plugin that were invalid. Next update I’ll have the output be made more human readable.

SVN Commit Notes:

In the “File Manager” screen: Added ability to sort columns ascending and descending. Removed last column “File Type”, since it was redundant–full file names are visible. Can be added back easily if requested, but cannot be sortable.

Moved the Bible array and initial Bible database and sermon reference ranges setup into a separate file for easier code reading in the main Sermon Posts class.

Modified several import queries to improve efficiency and accuracy, repaired the broken “Preachers” import procedure, and a section which associated verse ranges with sermons incorrectly. Cleaned up the core import procedure code a bit.

Renamed the sermon_biblebooks to sermon_biblebook in keeping with the singular table naming standard.

Additional Note:

I modified one of the tables, and the activate/deactivate procedure is what will update the table, so if you get a MySQL error in the sermon metabox, just deactivate and reactivate the plugin.

Plans for next update:

Display the import procedure output in a human readable form, and explain errors (most errors are from incorrectly stored verse ranges from the Sermon Browser plugin). Add an actual options screen.

Sermon Posts 0.7

Added support to attach a “Featured Image” to a sermon file. It would be easy enough, as a future upgrade, to add support to have a list of default images to select from (a dropdown list, for example), set through the settings panel.

The screen “Manage Files” is now functional, with filtering by the three taxonomies, Preacher, Service, and Series. A search box for searching by text is planned for a later update.

Deleting files is now possible, deleting them will remove them from the database completely, and will delete them from the server. This action is not reversible. A confirmation screen is set, so that deleting sermons requires an additional confirmation click.

The manage options page is not complete, but more options have been added, so that it is easier to change them.

Importing is still broken, that will be repaired for the next release.

Latest versions available here.

Things are moving along

I’ve been busy getting my blog here back into shape, getting pages put together and figuring out how to add a bug/feature tracking tool. I went with Trac, but because of various accidents on my end, I now have to wait for my host company to remove some files. It’s kind of awkward…

Site Changes: There is now a page with information handily available at tobiaslabs.com/sermonposts, and I’ll be separating the plugin news from the release notes, so if you only care about knowing when an update is released you can watch that page.

The location of the files is getting moved, as is the location of the SVN server, that way everything is in one handy place. Check out the main page for the info.