How to Add a Google Calendar to Your WordPress Page

by Lisa on June 24, 2010

Sometimes I need to put a calendar on client’s website. This is helpful for fitness trainers, event planners, etc. A Google Calendar is super easy for the client to update without logging into the website. It’s a handy tool.

Now, it’s not hard to embed a Google Calendar, because Google generates the code for you. However, WordPress makes it a little tricky. Here’s why.

A lot of my clients don’t know how to edit code, so they like to edit their pages and posts in the WordPress visual editor. The only problem with this is that the embed code from Google is an iframe, and WordPress doesn’t like iframes. The result is that when you add the code in the HTML editor and save the page, the calendar displays as planned. But if you then switch to the visual editor, the code gets deleted.

That’s pretty lame.

So what’s the fix? It involves a little custom coding, but it’s worth avoiding the hassle of re-creating your embed code every time you log onto that page in the visual editor by mistake.

I’m using the Thesis Theme for WordPress, and because you’re reading this blog I’m going to assume you’re using Thesis too. So here’s what to do:

In your custom_functions.php file, add this code:

function g_calendar() {
return 'PASTE YOUR GOOGLE CALENDAR EMBED CODE HERE. INCLUDE THE OPENING AND CLOSING BRACKETS FROM THE IFRAME' ;
}

add_shortcode ("gcalendar", "g_calendar") ;

Then using the HTML editor, add this code in the content area of a post or page – wherever you want the calendar to appear:

[gcalendar]

Save the post/page, and that’s it. Now when you need to edit, you can safely use the visual editor without breaking the calendar.

Are there any specific tips you’d like to hear more about? Leave them in the comments!

Share with a friend
  • Twitter
  • Facebook
  • LinkedIn
  • Digg
  • del.icio.us
  • StumbleUpon
  • Technorati
  • Print
  • email
  • RSS
  • Add to favorites

{ 4 comments… read them below or add one }

Tony October 6, 2010 at 3:50 pm

Bless you.

When I paste my Google cal embed code, should I keep the spaces you have after and before the // on either end of the line?

Probably even a dumber question: Keep the ” marks too?

Reason I’m asking: When I just tried copying that into my custom-function it busted it and returned an unexpected T-string error somehow..

Anything else required other than pasting the code into custom_functions?

Cheers, and thanks again for taking the initiative to do this!

Reply

Lisa October 6, 2010 at 3:59 pm

Hi Tony –

Yes, you need the slashes and everything there. Be sure to replace the part that says PASTE YOUR GOOGLE CALENDAR EMBED CODE HERE. INCLUDE THE OPENING AND CLOSING BRACKETS FROM THE IFRAME. The quotes around the iframe should be single quotes.

Hope that helps! If you still have trouble, give me the url to the page where you’re trying to add the calendar.

Reply

Kathleen April 5, 2011 at 3:38 am

I just tried this today (4/4/2011) and discovered a couple things I thought I’d pass along. First, if I left the slashes in, they appeared as text above and below my calendar. Second, in order to get the php to run, I had to use a single quote immediately before the open iframe tag and following the iframe end tag.

Reply

Lisa April 5, 2011 at 12:51 pm

Hi Kathleen – thanks for that feedback! It’s always interesting how things change over time. I’ve edited the post to show the current working setup – thanks for sharing!

Reply

Leave a Comment

CommentLuv badge

{ 2 trackbacks }

Previous post:

Next post: