Advertisement
eventsmanager

Add dates to permalink structure

Nov 7th, 2023
622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.42 KB | None | 0 0
  1. <?php
  2. /*
  3.  * Add the start event date in the permalink using the format /YYYY/mm/dd
  4.  * Inspired by http://pastebin.com/v4fT1671
  5.  * For installation instructions, see this tutorial, we recommend adding this to mu-plugins as a separate file:
  6.  * http://wp-events-plugin.com/tutorials/how-to-safely-add-php-code-to-wordpress/
  7.  */
  8.  
  9. /**
  10.  * Add the start event date in the permalink
  11.  *
  12.  * @param string $permalink Permalink
  13.  * @param object $post Post object
  14.  * @param boolean $leavename
  15.  * @return string Permalink
  16.  */
  17. function my_events_manager_get_permalink($permalink, $post, $leavename = false)
  18. {
  19.     if ($post->post_type == 'event') {
  20.         $event = em_get_event($post->ID, 'post_id');
  21.         $start_date = $event->event_start_date;
  22.         $start_date = str_replace('-', '/', $start_date);
  23.         $event_slug = get_site_option('dbem_cp_events_slug');
  24.         $permalink = str_replace($event_slug, $event_slug . '/' . $start_date, $permalink);
  25.         return $permalink;
  26.     } else
  27.         return $permalink;
  28. }
  29. add_filter('post_type_link', 'my_events_manager_get_permalink', 10, 3);
  30.  
  31. /**
  32.  * Add a rewrite rule to accept the date in the permalink
  33.  */
  34. function my_events_manager_add_rules() {
  35.     $event_slug = get_site_option('dbem_cp_events_slug');
  36.     add_rewrite_rule($event_slug . '/\d+/\d+/\d+/(.+)$', 'index.php?post_type=event&name=$matches[1]', 'top'); // single event
  37. }
  38. add_action('init', 'my_events_manager_add_rules', 9); // Must be run before events manager
Tags: permalinks
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement