Changing the event slug/permalink

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.

  1. Go to admin panel,
  2. Click on Permalinks under the Settings menu.
    Wp Event Manager Changing The Event Slug Or Permalink
  3. Select any Common Settings options.
  4. Save changes.

Add the following custom code to your theme functions.php to filter the permalink:

  1. function change_event_listing_slug( $args )

  2.     {

  3.         $args['rewrite']['slug'] = _x( 'events', 'Event permalink - resave permalinks after changing this', 'wp-event-manager' );

  4.         return $args;

  5.     }

  6.     add_filter( 'register_post_type_event_listing', 'change_event_listing_slug' );

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:

  1. function custom_submit_event_form_save_event_data( $data, $post_title, $post_content, $status, $values )

  2.     {

  3.         $event_slug = array();

  4.         // Prepend with organizer name         
  5. if ( ! empty( $values['organizer']['organizer_name'] ) )

  6.             $event_slug[] = $values['organizer']['organizer_name'];

  7.         // Prepend location if ( ! empty( $values['event']['event_location'] ) )

  8.             $event_slug[] = $values['event']['event_location'];

  9.             $event_slug[] = $post_title; $data['post_name'] = implode( '-', $event_slug ); return $data;

  10.     }

  11.     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

  1. function event_listing_post_type_link( $permalink, $post ) { // Abort if post is not a event if ( $post->post_type !== 'event_listing' )
  2.         return $permalink; 
  3.     // Abort early if the placeholder rewrite tag isn't in the generated URL

  4.     if ( false === strpos( $permalink, '%' ) )

  5.         return $permalink; 
  6.     $find = array(

  7.         '%post_id%'

  8.     );

  9.     $replace = array(

  10.         $post->ID

  11.     );
  12.     $replace = array_map( 'sanitize_title', $replace );
  13.      $permalink = str_replace( $find, $replace, $permalink );
  14.      return $permalink;

  15. }
  16. add_filter( 'post_type_link', 'event_listing_post_type_link', 10, 2 ); 
  17. function change_event_listing_slug( $args ) {

  18.   $args['rewrite']['slug'] = 'event/%post_id%';

  19.   return $args;

  20. }
  21. 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

  1. function event_listing_post_type_link( $permalink, $post ) { // Abort if post is not a event if ( $post->post_type !== 'event_listing' )
  2.         return $permalink; 
  3.     // Abort early if the placeholder rewrite tag isn't in the generated URL
  4.     if ( false === strpos( $permalink, '%' ) )
  5.         return $permalink;
  6.     // Get the custom taxonomy terms in use by this post
  7.     $terms = wp_get_post_terms( $post->ID, 'event_listing_category', array( 'orderby' => 'parent', 'order' => 'ASC' ) );
  8.     if ( empty( $terms ) ) {
  9.         // If no terms are assigned to this post, use a string instead (can't leave the placeholder there)
  10.         $event_listing_category = _x( 'uncat', 'slug' );
  11.     } else {
  12.         // Replace the placeholder rewrite tag with the first term's slug
  13.         $first_term = array_shift( $terms );
  14.         $event_listing_category = $first_term->slug;
  15.     }     
  16.     $find = array(
  17.         '%category%'
  18.     );
  19.      $replace = array(
  20.         $event_listing_category
  21.     ); 
  22.     $replace = array_map( 'sanitize_title', $replace );
  23.     $permalink = str_replace( $find, $replace, $permalink );
  24.      return $permalink;
  25. }
  26. add_filter( 'post_type_link', 'event_listing_post_type_link', 10, 2 ); 
  27. function change_event_listing_slug( $args ) {
  28.   $args['rewrite']['slug'] = 'event/%category%';
  29.   return $args;
  30. }
  31. 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

  1. function event_listing_post_type_link( $permalink, $post ) { // Abort if post is not a event if ( $post->post_type !== 'event_listing' ) {
  2.         return $permalink;
  3.     } 
  4.     // Abort early if the placeholder rewrite tag isn't in the generated URL
  5.     if ( false === strpos( $permalink, '%' ) ) {
  6.         return $permalink;
  7.     }
  8.        // Get the custom taxonomy terms in use by this post
  9.     $categories = wp_get_post_terms( $post->ID, 'event_listing_category', array( 'orderby' => 'parent', 'order' => 'ASC' ) );
  10.     $locations = wp_get_post_terms( $post->ID, 'event_listing_location', array( 'orderby' => 'parent', 'order' => 'ASC' ) );      
  11.     if ( empty( $categories ) ) {
  12.         // If no terms are assigned to this post, use a string instead (can't leave the placeholder there)
  13.         $event_listing_category = _x( 'uncategorized', 'slug' );
  14.     } else {
  15.         // Replace the placeholder rewrite tag with the first term's slug
  16.         $first_term = array_shift( $categories );
  17.         $event_listing_category = $first_term->slug;
  18.     } 
        if ( empty( $locations) ) {
  19.         // If no terms are assigned to this post, use a string instead (can't leave the placeholder there)
  20.         $event_listing_location = _x( 'anywhere', 'slug' );
  21.     } else {
  22.         // Replace the placeholder rewrite tag with the first term's slug
  23.         $first_term = array_shift( $locations);
  24.         $event_listing_location = $first_term->slug;
  25.     }
  26.     $find = array(
  27.         '%category%',
  28.         '%location%'
  29.     );
  30.     $replace = array(
  31.         $event_listing_category,
  32.         $event_listing_location
  33.     );
  34.     $replace = array_map( 'sanitize_title', $replace );
  35.     $permalink = str_replace( $find, $replace, $permalink );
  36.     return $permalink;
  37. }
  38. add_filter( 'post_type_link', 'event_listing_post_type_link', 10, 2 );
  39.  function change_event_listing_slug( $args ) {
  40.   $args['rewrite']['slug'] = 'event/%category%/%location%';
  41.   return $args;
  42. }
  43. add_filter( 'register_post_type_event_listing', 'change_event_listing_slug' );
  44.  function add_location_endpoint_tag() {
  45.     add_rewrite_tag( '%location%', '([^/]*)' );
  46. }
  47. add_action( 'init', 'add_location_endpoint_tag' );