fbpx

How can we help?

Changing the event slug/permalink

In case you want to change or customize the event slug or permalink of your Events, which otherwise on default settings would appear like, for example, http://yoursite.com/events/event-listing-title. There are 2 methods to customize this by WP Event Manager:

  • Using a localization file and translating the string.
  • Using a filter

Important note: To make the event slug functional, you will have to save it again after the change.

  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.

Use Filters to change the permalink base

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

<?php 
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 event slug/permalinks extraordinary, the event slug has organizer and event location names appended 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:

<?php 
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 this snippet.E.g. /event/1234/event-title

<?php function event_listing_post_type_link( $permalink, $post ) { // Abort if post is not a event if ( $post-&gt;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

<?php function event_listing_post_type_link( $permalink, $post ) { // Abort if post is not a event if ( $post-&gt;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-&gt;ID, 'event_listing_category', array( 'orderby' =&gt; 'parent', 'order' =&gt; '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-&gt;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 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

<?php function event_listing_post_type_link( $permalink, $post ) { // Abort if post is not a event if ( $post-&gt;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-&gt;ID, 'event_listing_category', array( 'orderby' =&gt; 'parent', 'order' =&gt; 'ASC' ) );
    $locations = wp_get_post_terms( $post-&gt;ID, 'event_listing_location', array( 'orderby' =&gt; 'parent', 'order' =&gt; '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-&gt;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-&gt;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' );
 ?>

Was this article helpful?
How can we improve this article?
Quick Links