Change Events Calendar permalinks for specific category in WordPress

My current company uses the very popular WordPress plugin Event Calendar. They publicize their own webinars using this popular plugin. They were looking to change the permalinks only for webinars. Naturally this was a feature Events Calendar did not provide right out of the box. To accomplish it involved writing two functions: one to add a rewrite rule and another to change the event permalink. If you need a refresher on WordPress rewrite rules here is a good post I found that explain how they work.

After doing some research, here are the two working functions I came up with:

add_filter( 'post_type_link', 'change_webinar_links', 10, 2 );
function change_webinar_links( $link, $post) {
    if ( $post->post_type == 'tribe_events' && tribe_event_in_category('webinar') ) {
$link= trailingslashit( home_url('/webinars/'.$post->post_name ) );
}
return $link;
}

add_action( 'init', 'webinar_rewrite_rule', 5);
function webinar_rewrite_rule() {
        add_rewrite_rule( '^webinars/([^/]+)', 'index.php?tribe_events=$matches[1]&post_type=tribe_events&name=$matches[1]', 'top' );
}

I added the code to the theme’s function.php file. Even though the code is designed to work with Events Calendar, it’s generic enough where it could be easily modified to work with any custom post type, just remove the Events Calendar function ‘tribe_event_in_category’.

There are some things to keep in mind. One is the first argument in the ‘add_rewrite_rule’ function is what you want the url string to be. The second argument is the url string you want to find and change. This is what threw me off and I’m sure for many others as well. WordPress looks for a url that has the same structure as the function’s second argument, takes the value of $matches[1] and inserts it where the parentheses start and end in the function’s first argument. For example a single event page with an actual url of www.example.com/index.php?tribe_events=event-name&post_type=tribe_events&name=event-name gets changed to www.example.com/webinars/event-name/. The url string of ‘index.php?tribe_events…’ is part of the actual event page url before WordPress changes it to a more pleasing format (i.e. www.example.com/events/event-name).

I welcome any questions or comments.

Matthew Dailey

Web developer, photographer, and Photoshop user.