Plugin Bugfix

It has come to my attention that my plugin was generating a PHP warning “Warning: Missing argument 2 for flh_wpcomgms_search_map_code()”

Although it doesn’t hurt anything, the warning is ugly.

Reading the documentation of the wp_insert_post_data filter more closely revealed the problem.

You must pass 2 for the $accepted_args variable in add_filter() if you want to access $postarr.

So I did that, and the warning went away.

However, when updating the plugin, I ran into a problem. The correct version was made available, but the text on the download button still referred to the old version number. I emailed plugins support, and discovered that I had forgotten to change the version number in the PHP file itself. I had only changed the ‘Stable Tag’ field in the readme.txt file.

So remember, when updating a plugin, it’s necessary to change both the ‘Stable Tag’ in the readme.txt file as well as the version number in the PHP file header.

Placeholder Menu Tab

The twentyten theme my site theme is based on using dynamic menus. This allows menus to be created and edited very easily by going to Appearance -> Menus and indulging in some fun drag and drop. Unfortunately, every entry has to link to somewhere (or so I thought). Thus I had pages for all the tabs and had to force some content into them.

I received a new requirement today. The ‘About’ page had to be renamed, but when grouped with another page, it made sense for both of those pages to be under a main heading of ‘About’. How could I create a placeholder menu tab which doesn’t link anywhere?

A quick Google search returned the solution in seconds.

Simply create a custom link with a target of ‘#’. This will create a link which doesn’t go anywhere and the menu entry can now be used as a top level tab.

Adding another Custom Post Type

A new requirement came in for the site I manage. There was a need to add about 3 years worth of question and answer PDF files to the site. The nature of this was similar to the newsletters. Each post would contain a link to a PDF file. The title would hold the question, but the writing area would not be deactivated so the question could be split if it is too long.

It is very likely that there would be similar files in the future, so I decided to make this a custom post type so a non techie administrator can handle future uploads. I considered doing this as a plugin, but I’m still not sure how to include page templates as part of plugins, so I decided to just incorporate it into my custom theme, like the other post types.

The first thing to do was to register the post type using the register_post_type() function. Straightforward stuff.

In the following paragraphs, {cpt} should be replaced with the name of the post type. If the custom post type is named “Books”, loop-{cpt}.php means loop-books.php.

The loop-{cpt}.php template file is where I customize the look of each custom post type. This is where I choose what to display and how to show it. In this case, I printed the words “Download the answer: “, followed by a link to the PDF file with the answer. As this is handled by the template, the look of every post will be consistent and this will not depend on the uploader entering the proper wording into the content area.

Next came the {cpt}-template.php file. This modifies the query so it only selects posts of the custom post type I’m interested in displaying. It then calls the loop template file using get_template_part( 'loop', {cpt} ); A blank page is then created in the admin area and assigned this page template. A link is added to the menu so this page shows up in the menu system.

A single-{cpt}.php file is also created. It calls the loop template using get_template_part() as well.

I also had to edit the sidebar.php file. My sidebar shows date archives for each custom post type. Which one to show depends on which post type is currently displayed. I registered a new widget area in functions.php, then modified the if-elseif statements in the sidebar.php file so it could support new post type. To see which post type is currently displayed, the get_post_type() WordPress function is used. I can’t remember what I was previously using, but it wasn’t good enough.

Another change was to style.css. Instead of targeting each widget area by ID to make them align right and set the sidebar width, I realized I could just use the widget-area class instead.

On the admin side, a meta box had to be added. I had already done this before so it was a simple matter of copying and editing the existing code. The meta_box class described in earlier posts is flexible enough to handle this with no problems. I did make sure to look through the save() function to make sure it could support the new file name format without any unpleasant surprises.

After all these code additions, I now have a new custom post type I can use to add question and answer PDF files through the admin interface. I added the existing content manually, and will teach the administrator to upload future ones.

The new custom post type gave 404 errors when first used. I found that it was necessary to place flush_rewrite_rules() into the functions.php file as the usual method of resaving the permalinks didn’t work. However, after refreshing the page, the 404 errors went away and I could take out the flush_rewrite_rules() line. It is important to remove this line once the site works, or the site will be severely slowed by the constant regeneration of rewrite rules.