How to Embed Google Calendar in a WordPress Website with Scrolling
Scrolling is no longer a design fad, it’s a useful tool that can enhance the user’s experience on your website. With scrolling, you can show more content on a single page without having to load more posts or pages. The content is displayed in a vertical fashion, with the most recent information at the top and older content below it. This means that as the user scrolls down, they will always see the most recent posts or events, updating as they scroll.
It’s quite easy to implement scrolling in your WordPress website; all you need is the code below. When implemented, Google Calendar will automatically display events from the user’s account in a vertical manner, scrolling as the user scrolls down. The event content will be displayed above the fold (i.e. the part of the page that is visible when the user first loads the page), keeping the reader engaged and allowing them to see more content without having to click a button or navigate to another page.
The Code
To start, we need to add the following code to our Header.php file in our WordPress theme:
<!—->
// Use Google Calendar API to get calendar events from the current user’s account.
<!—->
<?php
// You can get the Google API key from the Google Developer Console.
// If you don’t have a Google account, you can create one from the dashboard.
// Then, add the scrolled Events API to the WP_Query object.
<!—->
// If you’re using a PHP version older than 7, replace WP_Query with WP_List_Posts.
// Or, if you’re using a PHP version 7 or later, replace WP_Query with WP_List_Posts and WP_Widget with WP_Embed.
<!—->
// To embed content from a different website, you need to use the src attribute and value.
// If you leave out the src attribute, the content will be fetched from the current URL.
// For example, let’s say we want to display the Twitter timeline of the current user. We can add this line of code to our theme’s Header.php:
<!—->
<?php
echo ‘<p>’. do_shortcode(‘[twitter follow=”‘. get_the_author_meta(‘ID’). ‘” description=”‘. get_post_meta(‘description’, true). ‘” username=”‘. get_the_author_meta(‘username’). ‘” src=”‘. get_bloginfo(‘url’). ‘/wp-content/uploads/’. get_the_post_thumbnail($post->ID, ‘full’). ‘”>’;
</p>
<!—->
// Let’s say we want to display the Google Calendar events from the current user’s account. We can add this line of code to our theme’s Header.php:
<!—->
<?php
echo ‘<p>’. do_shortcode(‘[google_calendar events=”‘. get_the_author_meta(‘GCID’). ‘” for=”‘. get_the_author_meta(‘ID’). ‘” description=”‘. get_post_meta(‘description’, true). ‘”>’;
</p>
<!—->
// Let’s say we want to display the Twitter timeline of the current user. We can add this line of code to our theme’s Header.php:
<!—->
<?php
echo ‘<p>’. do_shortcode(‘[twitter follow=”‘. get_the_author_meta(‘ID’). ‘” description=”‘. get_post_meta(‘description’, true). ‘” username=”‘. get_the_author_meta(‘username’). ‘” src=”‘. get_bloginfo(‘url’). ‘/wp-content/uploads/’. get_the_post_thumbnail($post->ID, ‘full’). ‘”>’;
</p>
<!—->
// To display the Google Calendar events from the current user’s account, add this line of code to our theme’s Header.php:
<!—->
<?php
echo ‘<p>’. do_shortcode(‘[google_calendar events=”‘. get_the_author_meta(‘GCID’). ‘” for=”‘. get_the_author_meta(‘ID’). ‘”>’);
</p>
<!—->
// Let’s say we want to display the Twitter timeline of the current user. We can add this line of code to our theme’s Header.php:
<!—->
<?php
echo ‘<p>’. do_shortcode(‘[twitter follow=”‘. get_the_author_meta(‘ID’). ‘” description=”‘. get_post_meta(‘description’, true). ‘” username=”‘. get_the_author_meta(‘username’). ‘” src=”‘. get_bloginfo(‘url’). ‘/wp-content/uploads/’. get_the_post_thumbnail($post->ID, ‘full’). ‘”>’;
</p>
<!—->
// We can use the JavaScript ScrollTo plugin to smoothly scroll the content to the top when a user clicks on a headline or an event from the Google Calendar. To do this, add the following code in our theme’s Header.php file:
<!—->
// Use the plugin ScrollTo to create a special anchor tag (a tag) with the class ScrollTo and an ID of scrollTop. This tag will act as a trigger to scroll from the current location to the top of the page.
<?php
echo ‘<p>’. do_shortcode(‘[nav menu=”‘. get_the_author_meta(‘ID’). ‘” section=”‘. get_the_author_meta(‘ID’). ‘”>’);
</p>
<!—->
// When the user clicks on this link or on an event from the Google Calendar, the page will scroll to the top. This will ensure that they always see the most recent posts or events, as they navigate in the site.
<!—->
<?php
echo ‘<p>’. do_shortcode(‘[nav menu=”‘. get_the_author_meta(‘ID’). ‘” section=”‘. get_the_author_meta(‘ID’). ‘”>’);
</p>
<!—->
// To make our posts’ thumbnails larger, add the code below to our theme’s Header.php file: