Changing the event slug/permalink
In this guide, we will show you how you can change or customize the event slug or permalink. It usually appears like this:
http://yoursite.com/events/event-listing-title in the default settings. To customize this with WP Event Manager, you can use the following methods.
- Using a localization file and translating the string.
- Using a filter.
Important note: To make the event slug functional, you need to save it again after making changes to it.
- Go to admin panel,
- Click on Permalinks under the Settings menu.
- Select any Common Settings options.
- Save changes.
Use Filters to change the permalink base
Add the following custom code to your theme functions.php to filter the permalink:
function change_event_listing_slug( $args ) { $args['rewrite']['slug'] = _x( 'events', 'Event permalink - resave permalinks after changing this', 'wp-event-manager' ); return $args; } add_filter( 'register_post_type_event_listing', 'change_event_listing_slug' );
Change the new event slug/permalink
To make the event slug/permalinks extraordinary, the event slug has organizer and event location names added to it. You can alter this through a filter and custom function added to your theme functions.php file. The code used is as follows:
function custom_submit_event_form_save_event_data( $data, $post_title, $post_content, $status, $values ) { $event_slug = array(); // Prepend with organizer name if ( ! empty( $values['organizer']['organizer_name'] ) ) $event_slug[] = $values['organizer']['organizer_name']; // Prepend location if ( ! empty( $values['event']['event_location'] ) ) $event_slug[] = $values['event']['event_location']; $event_slug[] = $post_title; $data['post_name'] = implode( '-', $event_slug ); return $data; } add_filter( 'submit_event_form_save_event_data', 'custom_submit_event_form_save_event_data', 10, 5 );
Example: Adding the Event ID to the base URL
The event ID will be added to the base URL with the help of the following snippet.E.g. /event/1234/event-title
function event_listing_post_type_link( $permalink, $post ) { // Abort if post is not a event if ( $post->post_type !== 'event_listing' ) return $permalink; // Abort early if the placeholder rewrite tag isn't in the generated URL if ( false === strpos( $permalink, '%' ) ) return $permalink; $find = array( '%post_id%' ); $replace = array( $post->ID ); $replace = array_map( 'sanitize_title', $replace ); $permalink = str_replace( $find, $replace, $permalink ); return $permalink; } add_filter( 'post_type_link', 'event_listing_post_type_link', 10, 2 ); function change_event_listing_slug( $args ) { $args['rewrite']['slug'] = 'event/%post_id%'; return $args; } add_filter( 'register_post_type_event_listing', 'change_event_listing_slug' );
Example: Add the category to the base URL
The Event’s category name is added to the base url with the help of this snippet. E.g./event/event-category/event-title
function event_listing_post_type_link( $permalink, $post ) { // Abort if post is not a event if ( $post->post_type !== 'event_listing' ) return $permalink; // Abort early if the placeholder rewrite tag isn't in the generated URL if ( false === strpos( $permalink, '%' ) ) return $permalink; // Get the custom taxonomy terms in use by this post $terms = wp_get_post_terms( $post->ID, 'event_listing_category', array( 'orderby' => 'parent', 'order' => 'ASC' ) ); if ( empty( $terms ) ) { // If no terms are assigned to this post, use a string instead (can't leave the placeholder there) $event_listing_category = _x( 'uncat', 'slug' ); } else { // Replace the placeholder rewrite tag with the first term's slug $first_term = array_shift( $terms ); $event_listing_category = $first_term->slug; } $find = array( '%category%' ); $replace = array( $event_listing_category ); $replace = array_map( 'sanitize_title', $replace ); $permalink = str_replace( $find, $replace, $permalink ); return $permalink; } add_filter( 'post_type_link', 'event_listing_post_type_link', 10, 2 ); function change_event_listing_slug( $args ) { $args['rewrite']['slug'] = 'event/%category%'; return $args; } add_filter( 'register_post_type_event_listing', 'change_event_listing_slug' );
Example: Adding the event category and event location to the base URL
This snippet would add the category name and event location name to the base url. E.g. /event/event-category/event-location/event-title
function event_listing_post_type_link( $permalink, $post ) { // Abort if post is not a event if ( $post->post_type !== 'event_listing' ) { return $permalink; } // Abort early if the placeholder rewrite tag isn't in the generated URL if ( false === strpos( $permalink, '%' ) ) { return $permalink; } // Get the custom taxonomy terms in use by this post $categories = wp_get_post_terms( $post->ID, 'event_listing_category', array( 'orderby' => 'parent', 'order' => 'ASC' ) ); $locations = wp_get_post_terms( $post->ID, 'event_listing_location', array( 'orderby' => 'parent', 'order' => 'ASC' ) ); if ( empty( $categories ) ) { // If no terms are assigned to this post, use a string instead (can't leave the placeholder there) $event_listing_category = _x( 'uncategorized', 'slug' ); } else { // Replace the placeholder rewrite tag with the first term's slug $first_term = array_shift( $categories ); $event_listing_category = $first_term->slug; } if ( empty( $locations) ) { // If no terms are assigned to this post, use a string instead (can't leave the placeholder there) $event_listing_location = _x( 'anywhere', 'slug' ); } else { // Replace the placeholder rewrite tag with the first term's slug $first_term = array_shift( $locations); $event_listing_location = $first_term->slug; } $find = array( '%category%', '%location%' ); $replace = array( $event_listing_category, $event_listing_location ); $replace = array_map( 'sanitize_title', $replace ); $permalink = str_replace( $find, $replace, $permalink ); return $permalink; } add_filter( 'post_type_link', 'event_listing_post_type_link', 10, 2 ); function change_event_listing_slug( $args ) { $args['rewrite']['slug'] = 'event/%category%/%location%'; return $args; } add_filter( 'register_post_type_event_listing', 'change_event_listing_slug' ); function add_location_endpoint_tag() { add_rewrite_tag( '%location%', '([^/]*)' ); } add_action( 'init', 'add_location_endpoint_tag' );