Intrepid readers may have noticed the link to the “Upcoming Events” on the right sidebar underneath my site search box. What is that you ask? Well, since I’m always posting about shows, and then constantly forgetting about them by the time they roll around, I wanted to devise a way to enter the show as part of a post, and then have it show up magically in a list of upcoming events. So, with that in mind, I created a little hack for WordPress using some php code and the Custom Field Template plugin by Hiroaki Miyashita. If you were wondering how it all works, I’ll give a brief explanation here and a link to all the code you need. First of all download the following two items:
DOWNLOAD: Custom Field Template Plugin, Meta Data Event Calendar
Fig. 1 – Custom Field Template Plugin with Event Calendar Settings
First things first, download and install the Custom Field Template plugin for WordPress. The CFT provides a nice GUI interface for adding metadata to each post which is what drives the event calendar. While you could do what I’m doing without the CFT plugin, it would be much, much more time consuming. Once you’ve got the CFT template installed and activated, you’re going to want to configure it a little in the plugin settings. I’ve got my plugin configured specifically for New York area events, and you can look at download my settings over here, of course feel free to change the options based on where you live. (I think the settings are more or less self explanatory, especially once you look at them in the context of adding a new post.)
Article continued after the jump.
Fig. 2 – Add The Calendar Template and Calendar Page
Once you’ve got the settings configured to your liking, install the calendar.php template in your active theme folder (eg. wp-content/themes/youractivetheme) on your webserver. Then the last thing you need to do is add a new page to your blog (Pages-> Add New) and make sure that “Calendar” is set as the default template. You’ll see this setting on the right hand side of your page (see Fig. 2 above). This is very important, if you don’t set the default template to calendar, the event calendar will not work.
Fig. 3 – Add A Sample Event (aka New Blog Post)
Once you’ve configured the Custom Field Template and added the new Calendar page, there really isn’t a lot to do other than commence normal blogging. Just remember though, the next time you add a post that references a future event, add in the details in the custom fields below the post and voila, it will appear on your calendar page like magic. Events are ordered sequentially by date (dates should be formatted like “2009/12/31” or “2009-12-31”) and only future events show up in the calendar. Past events are still available in the database, and adding an archive page would be easy to do, but right now I don’t really see the need for it.
This event calendar isn’t the most robust thing in the world and it’s not meant to be. It’s just a quick little way to keep of track of shows without having to navigate off of the “add new post” page in wordpress. If you’re looking for something a little more hardcore, you might want to check out the Blogs for Bands WordPress Plugin. If there is anything you can think of that might make this calendar better, feel free to let me know in the comments. Enjoy!
A Final Note: When creating an event, I don’t recommend combining a ticketmaster or ticketweb link together with the “other ticket link” textbox. Just use one or the other, if you use both, two “buy ticket” links will show up under the event. If you choose Ticketmaster or Ticketweb as the ticket provider, the calendar performs an automatic search for your show on the respective site. If your show isn’t sold through the Ticketmaster or Ticketweb, then you should use the “other ticket link” textbox and paste the full URL to the ticket purchasing page.
UPDATE: I just added the code to generate a calendar RSS feed to the download package. To activate the RSS feed on your blog, all you have to do is add calendar_rss.php to your theme and then make a new WordPress page with “Calendar RSS” as the default template.
It doesn’t really handle recurring events at this point, though if you’re handy with the PHP you could probably add some code in there to deal with them in no time at all. It’s actually a great suggestion and I think I’ll probably add it in to a future release (whenever that may be).
Great code, thanks a lot.
I was able to do the part with the sorting by custom field, but I didn’t knew how to display only the upcoming events.
Not quite sure what you mean by your comment, but feel free to send me an email me about your issue and I’ll see what I can do.
Hi there. I mean I was able to do a part myself, but I never figured out how to display only the upcoming events, but thanks to your code now it’s very easy. Thanks again.
This is a really great mashup. Thanks a lot.
One thing, I was able to get the main Upcoming Events page to show up and populate properly, but I wasn’t sure how you created the small right hand “Upcoming Events” sidebar list that links to the main Upcoming Events page.
The sidebar code more or less makes use of the same code that is in the main events page, I’ll post an update soon.
I added an addendum to this post, dates should actually be formatted like “yyyy/mm/dd” for proper ordering.
Hi, this looks like exactly what I’m looking for, thanks! 🙂 I have one question – is it possible to show the present event (exhibition in my case) somewhere? I should have exhibitions page which shows the present exhibition and links to the past and becoming exhibitions.
Actually I think I already found a solution, I just have to modify line 83 to if($show_date = $today), that way it would show only the current exhibition, right?
ah, the code doesn’t show up correctly here. anyway the idea was to compare today’s date both to the end date and start date. If it is smaller (=) than start date it will show the post.
sorry for this mess, even the “less than” and “greater than” marking doesn’t show up here. Anyway, my modified code shows the event only if the start date is less than/equals today’s date AND the end date is greater than/equals today’s date. Simple really.
Hi Lariza,
Sorry I have missed your comments up until now, but it sounds like you got it figured out. Can you send me a link to your site?
Thanks for the post. I used some information here to help me find the solution for a project I’m working on (though I did it a bit differently).
I noticed you are using a comparison in your PHP loop to pick out all items in the future. You can do this much more efficiently by adding a comparison to your query_posts string:
.’meta_compare=>=&meta_value=’. date(‘Y/m/d’, current_time(‘timestamp’)
This will only return posts with date greater than or equal to the current date, so you won’t have all of the earlier posts in your loop. There could end up being a lot of old posts after a while.
Thanks Toby, great suggestion.
Wow, this is great! I’ve been working on and off on an event calendar, and now, I’m finally going to finish it. Thanks for the sample, and thanks to Toby for the database query optimization!
Anca, please send me a link when you get it up.
Thanks so much for this post. It has been a great help. I tried customizing four different event calendar plugins and failed. This route is working.
I’m only getting a Parse error: syntax error, unexpected T_ENDWHILE
on the events page. I’ve tried on different themes too…
Sounds like you are missing a “}”, or I am.
I am also getting an error:
Parse error: syntax error, unexpected T_ENDWHILE in …\wp-content\themes\twentyten\calendar.php on line 188
Is there a fix for it? I’d love to get this working as I think it’s a great idea.
@guy I haven’t checked the code lately, but it sounds like you are missing a “}â€, or I am.
Any idea where? I’m not a programmer really so any clues would be appreciated 🙂
Check the line number of the error message.
It was just referring to a blank line. Anyway I have found another way of doing this thanks to this plugin:
http://cstart.blogspot.com/2010/05/ajax-booking-availability-calendar.html
thanks anyway.