WordPress as a CMS, Part 6

Adding Print and E-Mail Features

If you’re just tuning in, this is the sixth installment of my series of articles about using WordPress as a CMS to build an informational Web site. Throughout this series, I’m talking about a specific site I developed: Flying M Air, a helicopter tour and charter company I operate when I’m not writing and tweaking my Web sites.

Because Flying M Air’s Web site was designed to be a sort of “online brochure” for my company’s services, I wanted the site’s visitors to be able to print information right from the site’s pages. Unfortunately, printing a WordPress blog page doesn’t seem to come out as pretty as it does on the site. Try it for yourself and see.

Although I could create PDF brochures for every tour and charter, it would be a royal pain in the you-know-what to recreate these documents every time I had a price change. To make matters worse, there was no way to force visitors to download the nicely formatted brochures instead of simply using their browser’s Print command to print what they saw in the browser window.

Clearly, I needed a better solution.

As usual, a plugin already existed to do the job: WP-Print by GaMerZ (AKA, Lester Chan). This plugin enables you to include a Print link or icon within The Loop for each post. When a user clicks the link or icon, the plugin generates a printer friendly page.

WP-Print is pretty easy to use. Just drop the plugin’s print folder into your plugins folder, go into the Plugins Management Administration Panel, and activate the WP-Print plugin.

Print OptionsNext, configure the plugin in the Print Options administration panel (Options > Print). You use the drop-down lists to indicate whether you want the user to be able to print comments, links, and/or images. I turned off images because they looked ugly with the yellow border around them. Click Update Options. You’re almost done.

Open the template file that includes The Loop and insert the following code where you want the Print link to appear:

<?php if(function_exists('wp_print')) { print_link(); } ?>

If you prefer a print icon like I have, no problem. Use this code instead:

<?php if(function_exists('wp_print')) { print_link_image(); } ?>

A word here about the Print icon that comes with WP-Print. It assumes you have a white background. I don’t. So I had to create my own little print icon. (I did the same for the e-mail icon, which I discuss later.) If you have to go this route, make it roughly the same size and give it the same name as the Print icon that comes with WP-Print and stick it in plugins/print/images/ to replace the existing icon.

If your theme uses The Loop in more than one place, you need to insert this code wherever you want the Print link or button to appear. For example, in my theme (Andreas), I had to insert it in index.php, archive.php, and single.php. I could have stuck it in page.php, too, but I didn’t need to, for reasons I’ll discuss in another installment of this series. In my aneclecticmind.com site, which uses the Exquisite theme, I just had to stick it in the post.php file, which is called by all other files that need to display posts. A much nicer solution, if you ask me.

While I was adding plugins and code, I figured I may as well add an e-mail feature. GaMerZ has one of those, too: WP-Email. This plugin enables you to put an E-mail link or button in The Loop for each post. Clicking the link or button displays a form the visitor can use to e-mail the post’s contents to someone else — or himself.

As you might expect, the plugin installs the same way. Put the email folder in the plugins folder and activate the plugin in the Plugin Management administration panel.

Configure the plugin in the E-Mail Options window (E-mail > E-Mail Options). I won’t show them here because they’re too big to display on this page, but they’re pretty self-explanitory. You can get more information in the documentation. Just make sure you select PHP for the Method Used To Send E-Mail if you’re not using an SMTP server.

Of course, when I installed it, I skipped the configuration step, which caused me to do a whole bunch of other work…more on that in a moment.

Now open the template file that includes The Loop and insert the following code where you want the E-mail link to appear:

<?php if(function_exists('wp-email')) { e-mail_link(); } ?>

If you prefer a e-mail icon like I have, no problem. Use this code instead:

<?php if(function_exists('wp-email')) { email_link_image(); } ?>

Remember, I modified the e-mail icon so it would have that silly yellow background that appears on my site.

E-Mail FormNow here’s where I screwed up — and only because I didn’t follow the instructions. I went onto the site and clicked the E-mail button for a post. A form like the one shown here appeared. The only problem was, there was no image in the Image Verification area. Why? Because the version of PHP I was using didn’t support captcha.

If I’d followed the instructions, I would have seen that that feature could be disabled. But I didn’t see the configuration options at all. So I did things the hard way. I upgraded PHP. And let me tell you, it was a scary thing to do. I actually lost all my blogs for about 10 minutes. Then I figured out how to get them back and everything is working fine. As an added bonus, I can now use the captcha feature in Spam Karma, which has cut my comment moderation work down to zilch.

Of course, I soon realized that I could only send one message every few minutes, which made testing tough. I found the configuration options and after slapping myself on the side of the head, adjusted the settings to give the new feature a good testing.

The moral of this story is RTFM — read the FABULOUS manual. (Gotta keep this site PG rated.)

While I’m discussing The Loop (kind of), I want to mention that I disabled the display of comments (and even comments links) in the Loop. I did this by commenting out the php comments_popup_link tag. I could have deleted it, but I was worried that I might need it someday and not be able to enter it back correctly. I already disabled comments and trackbacks in all posts and pages by setting defaults (as we discuss in our WordPress book).

Why did I do this? Remember, this blog is being used as a CMS. Although I could have left posts open for comments, I didn’t want to deal with Comment spam or have site visitors ask questions using the Comments feature. Visitors on the site are able to contact me using a contact form — that’s something we cover in our book so I won’t be discussing it in detail in this series.

In the next installment of this series, I’ll tell you about the site’s static Home page and the trick I used to get it to appear without a heading. Tune in next week!

WordPress as a CMS, Part 5

Adding random images.

If you’re just tuning in, this is the fifth installment of my series of articles about using WordPress as a CMS to build an informational Web site. Throughout this series, I’m talking about a specific site I developed: Flying M Air, a helicopter tour and charter company I operate when I’m not writing and tweaking my Web sites.

I didn’t expect my site to have a lot of pages, but I did have a lot of pictures I wanted to show. One of the things I learned long ago is that every Web page needs a good mix of text and images. Not too much of either one. But rather than pick and choose a limited number of photos to illustrate my aircraft and destinations, I decided to use a more complex solution: set up the site so images would appear randomly on the pages in predetermined places.

I searched for a plugin that would help me do the job. The first one I found was the Random Image Plugin. This plugin randomly selects an image attached to a post using WordPress’s built-in upload feature and displays that image. This is not what I wanted to do.

I then found the Random File plugin by Scott Reilly. This plugin enables you to place a random file from within a specific folder in a theme’s template file or, with the help of a plugin like Exec-PHP, within a post or page itself. That’s exactly what I wanted to do.

I created the images in two basic sizes: small, for the left sidebar, and medium, for within posts and pages. The small images already existed in a predefined size in my old Web site on its sidebar. All I had to do was modify them to use them in the new site.

Come Fly with Me!The modifications weren’t difficult, but required some use of Photoshop. Since my brochure used a snapshot-like image format, with a white photo border and drop shadow, presented on a 3° angle (in either direction) I wanted the same format on my site. The angle and drop shadow required that the entire image be placed on a background the same color as my site’s page background: yellow. You can see an example here.

I created a few Photoshop actions that automated the lengthy process of resizing the image (for medium images), placing them on a background, creating the drop shadow, and tilting the whole thing one way or the other. I did this with all the images I wanted to use and saved them as JPEGs in topical folders on my Web server, within an images folder in the wp-content folder. For example, one folder had images of the helicopter in flight, another had the interior, another had destinations for day trips. You get the idea.

I installed and activated the plugin. It’s very straightforward, with only one file to upload to the server and no configuration required.

I also installed and activated the Exec-PHP plugin. We discuss this plugin in our WordPress book, so I won’t go into detail about it here.

Now I was ready to add the random images. For each page of content, I decided where I wanted the image to go and what kind of image I wanted to display. For example, on the Home Page I wanted to display a photo of the helicopter in flight. I had a folder with a few of those images in there, so all I had to do was insert the code that would display a random image from that folder:

<img src="<?php echo c2c_random_file('/wp-content/images/helicopterinflight/'); ?>" class="right" alt="Flying M Air's Helicopter in Flight" />

Keep in mind that I put this code in the body of the Page — not in the template file used to display the page. That’s why I needed Exec-PHP. You can’t include PHP in a post or Page without something like Exec-PHP to help out. (There are other plugins that do the same thing, but this happens to be the one I chose. With luck a reader of this article will point out some alternatives.)

Note the class="right" component of the tag. This uses CSS to position the image. For this to work, you must have right defined in your style.css file. This was predefined in the version of Andreas I used to base my site on, so I was all set. Otherwise, I could have done it the old fashioned way, with code like class="right" align="right" hspace="5". But the CSS approach is better, as Miraz could explain better than I could.

I did this throughout the Pages and posts on my site, wherever I wanted a random image. If I wanted the same image to appear every time in a specific spot, I just used a plain old img link to specify the image I wanted to appear.

Getting the smaller images in the sidebar was a bit tricker, but the Random Image plugin’s excellent documentation gave me the information I needed. The goal was to display a bunch of random images. If I just used the same code several times in a row, there was a chance that an image would be repeated in the column. I didn’t want that. So I used this code in the leftsidebar.php file:

<p><?php
$files = array();
for ($i=1;$i<=<strong>7</strong>;$i++) {
   $file = c2c_random_file('<strong>/wp-content/themes/andreas/images/small/</strong>', 'jpg png gif', 'url', $files);
   echo '<img src="' . $file . '" alt="Random image #' . $i . '" />';
   $files[] = $file;
}
?></p>

There are two configuration options to note here:

  • The number 7 in the code is the number of images to display. I keep playing with this number, so it might show a different number of images if you visit the site. My goal was to have the column of images approximately the same length as the navigation sidebar so they’d balance out the page.
  • The path to the folder of images to display tells Random Image where to find the images. In this case, I put the images in a folder in the theme folder. But I could have put them anywhere, including in the images folder with my other images.

To make this work in the sidebar, it’s important that all the images are the same size. Otherwise it’ll look weird. Of course, you might want it to look weird. I didn’t.

In the next installment of this series, I’ll tell you about the Print and E-mail features I added to help site visitors share information with others. See you next week!

WordPress as a CMS, Part 3

Planning the site’s organization and creating the Pages.

The primary difference between an informational Web site and a blog is that a Web site’s content stays pretty much the same from day to day while a blog’s content changes regularly when new posts are added. That’s one of the reasons that blog entries display the date and have date archives. This kind of functionality wasn’t what I needed for the new Flying M Air Web site.

WordPress, of course, offers the static Page feature which makes it possible to create content that exists outside the blog’s chronology. Unfortunately, pages don’t make use of the Category feature, which is another way that blog entries are organized.

Let me take a moment to explain the kind of information I needed to convey to site visitors and how I wanted to organize it.

  • First, I had a bunch of very static, unlikely to change information, such as the company background, contact information, and information about the helicopter. Although this could be presented using blog entries with a “General Information” category, I wanted to present it separately, using the page feature.
  • I also had descriptions for the tours, day trips, excursions, and other services I offer. I wanted to group these together by category, which seemed like a logical organization for me. Now although I could create a separate page for each description as a subpage under a parent page, I thought that approach would be difficult to manage. So I decided to use blog entries for each description, assigning one or more categories as necessary.

The theme I had chosen and modified (a version of Andreas, as I discussed in Part 2 of this series) had a very nice horizontal navigation bar at the bottom of the header, perfect for WordPress Pages. But I worried that site visitors might be confused by two separate and apparently unrelated navigation bars: the nice horizontal one on top and the vertical one in the right sidebar, which included the list of categories. If I didn’t include information about my services in the top navigation bar, site visitors might not find it. So I decided to create summary Pages that listed and described several categories of tours and services, providing category links for each one.

Here’s how it works. On the top navigation bar, there’s a Tours & Day Trips link. Clicking that link displays a WordPress Page that discusses three categories of services: Tours, Short Day Trips, and Arizona Day Trips. Clicking one of the “Learn More” links on that page displays the category with its tours.

With this approach, a visitor could find my services, no matter which navigation method he used.

Once I decided on an approach, it was time to create the pages. I created eight pages: Tours & Day Trips, Excursions, Charters & Other Services, Get Brochures, Q & A, About Us, About Our Helicopter, and Contact us.

Although I didn’t create the pages in that order, I wanted them to appear in that order. So I used the Page Order field in the Write Page administration panel to assign a page number to each page. I skipped numbers when numbering them so I could easily change the order by changing just one Page’s Page Order value.

The Get Brochures Page currently provides links to downloadable PDF files. I didn’t use the link feature or any special template for the Page — although I could have. It didn’t seem worth the extra effort. But now I’m thinking that might be a good idea so the downloadable links appear on every page. So if you look at the site and that Page is missing, check the sidebar for the Brochure links; I’ll probably move them there.

The Contact Us page provides contact information for Flying M Air as well as an e-mail contact form. The contact form was created with the WP-ContactForm plugin by Ryan Duff. Miraz discusses this plugin in Chapter 7 of our WordPress book, so I won’t go into detail about it here.

In the next installment of this series, I’ll tell you about the conditional statements I used in the sidebar to list all tours for the currently viewed category.

WP-Print

I add a post printing function to the site.

One of the things that bugs me about WordPress is that when you print a page, the resulting printout does not have the same formatting as the page in your Web browser. Instead, styles are pretty much stripped out and sidebar components are added to the end (at least in my case) to the printout. This makes the printout longer than it needs to be.

Enter Lester Chan‘s WP-Print plugin. It enables you to add a Print link to your posts. When a user clicks the link, the plugin creates a simply formatted page with the page contents and URLs. You can then click a link on that page to print the formatted page.

It’s a much nicer solution than just printing from WordPress.

Displaying Random Ads in Your WordPress Blog

Instructions for using the AdRotator plugin to generate random ad images with links.

I’ve been wanting to do this forever. In fact, it was the first thing I explored when I started using WordPress late last year. Unfortunately, I didn’t have enough understanding of how plugins and theme files worked to get anywhere.

Things are different now. I know what I’m doing. And this particular task is quite simple, as you’ll see here.

The Goal

My goal was to be able to display small advertising images at the top of the navigation bar. The image would appear randomly from a pool of images and, when it appeared, it would be associated with a specific URL. When the image appeared, the site visitor could click the image to go to a Web site or page.

I wanted to use this for wickenburg-az.com, a site I spend an awful lot of time working on with no compensation. The idea was to charge a nominal fee to add an image to the pool for a month. The money I collected would cover the cost of running the site.

Keep in mind that I could use any size image and place it anywhere in the header, sidebar, or footer. I wanted a 170 x 120 pixel image at the top of the sidebar. (I already run a strip of Google text ads at the bottom of the page, as I do here. I think too much advertising on a Web site is extremely obnoxious. But how much is too much when you have bills to pay?)

The Plugin

I won’t go into detail about my search for the plugin. It was neither long nor exciting. I wound up with AdRotator by Angsuman Chakraborty, which was really designed to work with Google AdSense and other prepacked blog advertising tools. But as I read the comments for the plugin’s description, I was assured that it would do what I needed to do.

But how do I use it? The User Manual was difficult to find. It wasn’t very user-friendly, either. (No offense to the plugin author. Frankly, I think that every programmer should team up with a tech writer to get documentation written. Programmers program. Writers write. Programmers don’t usually program and write — although an exception does come to mind.)

So I decided to write up a quick How To piece that would explain how to use AdRotator to do just what I wanted it to do. This isn’t AdRotator documentation. It only explains how to use the plugin for one particular task.

Also, these instructions don’t explain every single step in minute detail. If you want that kind of explanation, buy a Visual QuickStart Guide. Instead, these instruction assume you have already mastered the basics of using your image editing software, an FTP client, a text editor, and WordPress.

Create the Ads

First, use image editing software and your FTP program to create and upload the ad images.

  1. Decide on an ad size based on where you plan to place the ads. I used 170 wide by 120 tall so it would fit in the sidebar column of my theme (modified Connections).
  2. Use your favorite image editing software to create each image in the desired dimensions.
  3. Save the images as GIF, JPEG, or PNG format files with the appropriate file name extension.
  4. Use your favorite FTP client to upload the images to a folder within your WordPress wp-content folder. I created a folder called ads (real creative, huh?) and put them there. For obvious reasons, the folder’s permissions must be set so its contents are readable.
  5. Put away your image editing software; you’re done with it for now.

Install and Activate the Plugin

To use any WordPress plugin, you must download it, install it, and activate it.

  1. Download the plugin. This was pretty challenging, since I had a hard time finding a link to it. You can find a link on this page or simply click Download the plugin.
  2. Use your favorite FTP client to upload the AdRotator.php file to your plugins directory inside your wp-content directory.
  3. Go to the Plugins adminsitration panel in WordPress and activate the AdRotator plugin.

Create the Reference File

Now comes the part that seems to confuse most people (according to the comments). You need to create a plain text file that includes HTML for displaying the ad graphics with links to their URLs.

  1. Fire up your favorite text editor. Do not use a word processor like Microsoft Word! I use TextWrangler, which I can’t say enough nice things about.
  2. Create a new text file.
  3. For each ad/URL combination you want to include in the pool of ads, create a line in the text file that includes the following code:

    <a rel="external nofollow" href="http://advertiserurl.com" title='advertiser name or slogan'><img src='http://yoursite.com/wp-content/yourimagefolder/imagefile.jpg' border="0" /></a>

    Of course, you’ll substitute real URLs and text for what is shown above. A real example from my setup would be:

    <a rel="external nofollow" href="http://www.flyingmair.com" title='Helicopter Tours and Charters'><img src='http://www.wickenburg-az.com/wp-content/ads/flyingmair.jpg' border="0" /></a>

  4. Save the file with a Web-friendly name and the .txt file extension. In my example, I named it ads.txt (not very creative).
  5. Use your favorite FTP software to upload the file to the wp-content folder in your WordPress installation.

Reference the Ads

Now you need to reference the AdRotator plugin in the theme file in which you want the ad to appear. Normally, this will be header.php or footer.php (for banner ads) or sidebar.php (for ad boxes or towers).

  1. Open the theme file in which you want to reference the ad.
  2. Include the following code exactly where you want the ad to appear:

    <?php echo getad('adfilename'); ?>

    Of course, you’d replace adfilename with your file name excluding the .txt extension. So in my example, it would be:

    <?php echo getad('ads'); ?>

  3. Save the theme file you edited.

Check Your Work

That’s all there is to it. To check your work, display any blog page that uses the theme file you modified.

If you’re great at following instructions and I didn’t make any mistakes in the instructions here, it should work fine.

If you’re human (like I am), you’ll have to fix a number of tiny boo-boos you made along the way. The mistakes I made include wrong image URL (duh) and incorrect formatting (theme-dependent). I also made some ugly images the first time around and had to make new ones that looked better. I’m a writer, not an artist.

If you make some errors and your fixes don’t seem to be working, clear your browser’s cache. Then reload a page. It should be okay.

If you want to see how mine came out, visit http://www.wickenburg-az.com/.

Tips

A few tips for making advertisements less painful for site visitors:

  • Keep image file sizes small. I recommend less than 20K, but definitely less than 100K.
  • If the ad image is textual and you’re placing it on a colored or patterned background, consider saving the image as a GIF with transparency.
  • Animate GIFs look cool if properly done. Unfortunately, I can’t make one that isn’t terribly obnoxious.

And one tip for making advertisements more painful:

You can create multiple txt files, each with its own lists of images and links, and reference each one in a different place on your blog. This makes it possible to combine header, footer, and sidebar advertising on one site. Just don’t expect me to visit often; I really do hate ads.

Troubleshooting at 6 AM

A plugin goes bad and causes weird side effects.

I was checking e-mail and moderating comments on this blog early this morning. (I’m a morning person.)

I’d just entered a comment on my blog when I got a weird error message that I didn’t really pay attention to. When I moved on to another page, there were 8 lines of gibberish above the header. On every page I viewed. Before Firefox said, “To hell with this,” and crashed. Several times.

I had a problem.

Netscape could view the page — but with the gibberish.

wickenburg-az.com, which lives on the same server and uses the same software, worked fine. (Whew!) That narrowed it down a bit.

I restarted the computer. The problem didn’t go away.

I accessed the server from home and restarted Apache. The problem didn’t go away.

Dang.

I went to the office at 7:30 AM and restarted the server computer. The problem didn’t go away.

I analyzed the source code of the bad pages — which would appear on Firefox at the office. (That could be because my office computer has a faster, more modern processor and a heck of a lot more RAM.) It pointed to php files used by WordPress to display pages and do its magic. All the files looked right.

I looked at the contents of the database with CocoaMySQL. It looked okay. But then again, I didn’t look at all the contents. And I really didn’t know what to look at.

I thought it might be the gravatars plug in, which I’d been fiddling with tha morning. I disabled it. And I got a weird error message that might have been the same one I’d gotten earlier that morning. This time I paid attention. It mentioned duplicate headers and a couple of files by name. Hmm.

I took a deep breath and went online to WordPress support. I searched the Codex and support forums. I didn’t find an answer. So I posted a message in the support forum. I made the mistake of including the bad code, so when I saved it, the message got cut off at the bad code. Dumb. I used my back button to revise the message and post it. That created a second message with the same title. Really dumb.

But Samboll, another member, was right on it. He made two suggestions. The second one pointed to a Codex document that explained why the header error message might appear. I thought the header error message was a secondary problem — the first was the gibberish characters — so I didn’t have much hope in it solving the problem. But I had nothing else to go on, so I read the Codex piece and followed its instructions.

And that’s how I tracked down the bad file: srg_clean_archives.php, which is the plugin for the Archives feature I installed the other day. When I opened the file, it was filled with junk.

Now I don’t know how the file went bad, but there was no doubt in my mind that it was bad. It didn’t even show up in the list on the Plugins administration panel. I deleted it (it took two tries) and reloaded the site’s home page. Voila! Problem solved.

Of course, now my Archives page didn’t work right.

So I went back to Sporatic Nonsense’s Clean Archives 1.5 page and downloaded a fresh copy of the plugin. I installed it, activated it, and everything is back to normal.

Total time lost to this problem and troubleshooting (not counting the time to write about it): about 2 hours.

The moral of this story: pay attention to error messages.