WordPress Event Calendar Using Custom Post Types and Verbose Calendar

We use WordPress Event Calendar to show the posts created on each day. Also visitors would love to know about the future posts. I have seen some sites which provide the Title of future tutorials so that visitors can stay updated on the topics they like. Likewise we can use WordPress as an event management system as well. Schedules of events are much more important than blog posts. We need to provide the option for users to view events on a calendar. Even a site like the Tuts+ network can have so many events like the launch of a new Tuts+ site, the launch of an e-book, a monthly newsletter and many more.

So in this tutorial I am going to use a jQuery plugin called Verbose Calendar in combination with Custom Post Types to create a simple event calendar. Verbose Calendar plugin is developed by John Patrick Given and available for download on GitHub.


What Are We Developing Today

Verbose Event Calendar plugin will allow site owners to create events on the admin dashboard and display to users using a calendar.

  • Creating a Custom Post Type for events and adding events.
  • Create a shortcode to display jQuery Verbose Calendar.
  • Add events to the verbose calendar using AJAX.

Step 1 Creating the Plugin File

Initially we have to create the plugin folder in the /wp-content/plugins directory. I am going to create a folder called verbose-event-calendar as the plugin folder and create the index.php file with the necessary information about the plugin. Once created you can go to the plugins menu of the admin panel and activate the plugin using the activate link.


Step 2 Understanding the Folder Structure

Since I am going to use external jQuery plugins for this tutorial, it's important to have a good understanding about the folder and files structure. The following screen will show you a graphical view of the file structure of this plugin.

Folder Structure
  • images - contains all the images used for the WordPress plugin.
  • javascripts - JavaScript files for the jQuery Verbose Calendar plugin.
  • stylesheets - CSS files for the jQuery Verbose Calendar plugin.
  • themes - CSS files for jQuery Date Picker.
  • ui - JavaScript files for jQuery Date Picker.
  • verboseCalAdmin.js - custom JavaScript code related to the admin section.
  • verboseCalCustom.js - custom JavaScript code related to front end.
  • styles.css - custom styles for the plugin.
  • index.php - PHP code of the plugin.
  • README.md - README file for jQuery Verbose Calendar.

Step 3 Enqueue JavaScript and CSS Files

We need separate JavaScript and CSS files for the front end and admin sections. The following code will show you how to include them properly using the wp_enqueue_scripts action.

Include the JavaScript and CSS files to display the jQuery Verbose Calendar:

Include the JavaScript and CSS files for the admin interface:


Step 4 Register Custom Post Type for Events

Events can have many details like date, location, participants, etc. I am going to create a Custom Post Type called event considering the flexibility and extendability of the plugin. The first task would be to register a Custom Post Type with WordPress. Let's go through the code for registering Custom Post Types.

  • First I am going to make a function call on the init action to register a Custom Post Type. register_custom_event_type will be called.
  • Inside the function we have to define labels for the Custom Post Type. These variables defined in the $labels array will be used as the labels for the Event creation form and saving post types in the database. You can find more information about Custom Post Type labels under register_post_type in the WordPress Codex
  • Then we have to define a list of arguments to be passed into the register_post_type function. I am going to use default values for most of the arguments as shown in the above code.
  • I have done modifications to the supports argument. This is used to define the available form fields and components inside the event creation screen. In this plugin we need only Event Title and Description. So I have removed the other values like thumbnail, custom-fields, excerpt, etc.
  • Finally we pass the arguments for the register_post_type function with the name of our Custom Post Type as event.

"It's important that you keep your Custom Post Type name under 20 characters and with no capital letters or spaces."

Now you will be able to see a menu item called Events in the left menu of the admin panel before the Appearance menu. Once you click on it you will be redirected to an event creation screen with Event Title and Description fields. Next I'll show you how to add event specific fields to the form.


Step 5 Create Custom Events Fields

Events can have much information as I mentioned earlier. You have the ability to create your own fields according to the type of events you are using in the website. So I am going to create fields for starting date and ending date, which are common to any event type and let you create your own fields by modifying code. We can create our own fields inside meta boxes as well as default custom fields to store additional information. Since we have already removed custom fields from the events screen, I am going to use fields inside meta boxes to create starting and ending dates.

Add Event Info Meta Box

First we have to add a meta box as the container of our custom fields using the code below.

I have used the required parameters for the add_meta_box function in the above code. You have to pass a unique ID, meta box display title, function name for generating custom fields, and event type as the required parameters. You can find more details about the optional parameters under add_meta_box in the WordPress Codex.

Add Event Fields

We are going to add 2 fields for start date and end date of the event inside the meta box created earlier. HTML code for the form fields are generated inside the display_event_info_box function.

  • This HTML code will be used in both Add Event and Edit Event screens. So initially we have to get the current values of the fields using the get_post_custom function.
  • Next we create the nonce value to be checked in the event saving process.
  • Then we output the HTML code for the 2 text boxes for start and end date. If a value already exists, we assign it to the value attribute of input fields.

"I have named my meta keys for start and end event as _eve_sta_date and _eve_end_date. When you use '_' in front of meta keys, it will not be shown as a custom field. Otherwise we will get duplicate fields one inside the meta box and one with custom fields. Always make sure to prefix your meta keys with '_' in case you don't want it to be shown as a custom field."

Add Date Picker to Event Fields

Even though we created 2 text fields for start and end dates, it will be used to insert actual dates. So I am going to assign the jQuery Date Picker to text fields in order to allow users to select dates from a calendar without entering manually. We need to add some JavaScript code to the verboseCalAdmin.js file to assign date pickers to input fields.


Step 6 Validating Event Creation

We need to do some validation before saving events to database. So I am going to use jQuery validation on the event creation form as shown below. The below code is included inside the verboseCalAdmin.js file.

  • The jQuery function will be called on form submission using the form ID post.
  • There are a set of hidden fields in the post creation screen. I am using the value of the post_type hidden field to check the post type.
  • Title and Dates are required. So those fields are validated using jQuery.
  • Then we add custom error boxes to be displayed under each field.
  • If the form does not validate properly, errors will be displayed and event creation will be stopped until all the validation errors are fixed.

Step 7 Saving Events to Database

Once all the validation errors are corrected and the form is submitted successfully, we call the save_event_information function on the save_post action as shown in the following code.

  • This is the standard method of saving custom post meta to database. First we check if the function is called within WordPress autosave and return from the script.
  • Next we validate the nonce value generated in the form and nonce value submitted through $_POST.
  • Then we have to check whether the user has necessary permission to create events using the current_user_can('edit_post') function.
  • Finally we save both start date and end date to the wp_postmeta table in the database.

Now we have completed the process of creating events through the admin panel. Next we have to work on including the jQuery Verbose Calendar in the front end and display the events and posts to the users.


Step 8 Creating Shortcode for Verbose Calendar

First we need to create a shortcode which displays the Verbose Calendar on the page. Once you include the following code you can create a new page in the admin panel add the shortcode as [verbose_calendar/] to the editor to display the calendar on post.

The above shortcode code inserts HTML elements needed for the calendar. We have to load the calendar using jQuery as shown below. The following code is included inside the verboseCalCustom.js file.

If everything is done properly, you should have a calendar like the following image in the page you created.


Step 9 Assigning Events to Calendar

Next task of this tutorial is to query the events and posts from the database and display on the calendar. Default Verbose Calendar provides only the calendar layout. We have to customize the plugin in order to assign events to the calendar. Verbose Calendar uses a function called g.prototype.print to display the calendar. So we are going to customize this function to retrieve events and posts from the database as shown below. Code is located in the jquery.calendar.min.js file inside the javascripts folder.

  • I'll explain the codes we have changed and important from the plugin's perspective. Plugin specific code is omitted here.
  • First I have placed the whole code inside a AJAX request. This request will be made each time you change the year or load the calendar.
  • AJAX request will be made to the get_posts_for_year action. It will return list of posts and events for the current year displayed on the top of calendar.
  • Then we loop through each result using $jq.each method. We pass the details of result to the postDetailsArr array.
  • We use the start date for events and published date for posts and assigns to postArr array.
  • The above code snippet is also located inside the print function.
  • We prepare the date and month by adding 0 as the prefix in case it is less than 10.
  • Then we check each date exists inside the postArr using the inArray function.
  • If date contains an event or post, we add a special CSS class called event_highlight.

Generating Post and Event Lists

Now we have to query the database and generate results for the AJAX request. Consider the following code for the request generation process.

  • First we assign the action to both logged in users and normal users.
  • Then we get all the published posts for the selected year and assign it to the $allEvents array.
  • We assign the events for the selected year using same procedure as above and output in JSON format.

Step 10 Displaying Events List

Dates with events or posts will be highlighted in a blue color. Once you click on such a date, the events list should be displayed. Verbose Calendar has a function called calendar_callback which we are going to use in order to display the events list. The following JavaScript code inside the verboseCalCustom.js file will be used for this functionality.

  • The clicked date will be passed automatically to this function. First we assign the date to a variable using the passed date object.
  • Next we get the events and posts for the selected date using postDetailsArr generated in the print function.
  • Then we assign event and post details by considering the type variable.
  • Finally we assign all the HTML to the event_row_panel container and display the events list.

Conclusion

Once the tutorial is completed you will have a cool event calendar plugin. This contains only basic fields of events. You can create more information about events by adding more custom fields. Feel free to extend the functionality of this plugin and share your suggestions.

Tags:

Comments

Related Articles