I just recently discovered the Events Manager plugin for WordPress and implemented it into a client’s website along with the Events widget that comes with the plugin. The client liked the plugin and the widget, but wondered if the Events widget could be filtered by category to be able to display the events for a particular category on a page related to that category.
After reading through the documentation and posting a question on the plugin’s forum at WordPress.org, this is the solution I came up with.
The easy way: just modify the widget php file
I actually did this the slightly more difficult way to begin with, but looking back now I guess there’s no reason to not modify the widget and make the desired change all in one move. What makes this simpler than it may sound is that the developer has built the plugin for future modifications such as this one, which you can start reading about on this page on the Events Manager website.
The PHP file that creates the Events widget is em-events.php, and is found in the directory wp-content/plugins/events-manager/widgets. Once you’ve located that file using your FTP client (I use Filezilla), download it to your desktop and open it with your favorite text editor (I prefer Notepad++).
(Caveat: I’m not a coder, but I like to think that I’m working in that direction, and this exercise is one small step forward.)
Once you’ve got the file open and ready to edit, add the following commented line somewhere around line 14:
Add ‘category’ to the widget’s function
Next, add the following commented paragraph at approximately line 80:
Add the category option to the widget’s form
Save the file, FTP it back to its directory on your server, go to your widgets screen in your WordPress admin, and you should now have the option to specify one or more categories to be displayed. The default setting is “0” (zero), which displays all categories, and to display multiple categories separate the IDs with commas.
To learn more about how this customization potential is built-in to the plugin, check out Event Search Attributes in the plugin’s documentation.
The slightly more difficult way: clone and modify the widget
That was the easy way. To add a few more steps and have a separate widget for specifying categories, do the following:
- Make a copy of em-events.php and name it something like em-events-by-category.php
- In this new file find all instances of [highlight]EM_Widget[/highlight] and replace them with something like [highlight]EM_Widget_by_category[/highlight]
- Add the code in the two places as explained above in the previous section.
- Change the ‘title’, ‘description’, and name (see highlighted code below)
- FTP the file wp-content/plugins/events-manager/events-manager.php to your desktop and add the following at approximately line 58:
- FTP the new widget file and the events-manager.php file back to their directories.
- Go to your widgets screen in WordPress and verify that you have a working plugin that filters events by category.
Here is the code for the modified em-events-by-category.php file, with all the changes and additions commented.
Code for the modified em-events-manager.php file. Changes in lines 4,7,12,14,16,25-26,81-85,165 – click through to this Gist on GitGub to see line numbers.
nice tutorial, one comment/suggestion…for the harder way (recommended over the first one), instead of modifying any part of EM directly, you can just copy that widget file into your own theme file, and include it there e.g. within your functions.php file, or pasting it directy into the functions.php file, because it’s then upgrade-safe.
either that, or copy it into your mu-plugins folder and it’d get included regardless (but remember to delete if you delete EM)
Marcus, thanks for the tips. After I wrote this tutorial the developer incorporated the change into the plugin, rendering this post sort of obsolete. What you suggested is a better way to manage changes though, and lately I’ve been using similar methods for modifications.
i am the developer 🙂
That comment made my morning! I’m still smiling…
Thanks for your input – good to see a developer who stays involved!
That comment made my morning! I’m still smiling…
Thanks for your input – good to see a developer who stays involved!