Advertisement
Guest User

Untitled

a guest
May 19th, 2012
677
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. Plugin Name: FG Events Manager Hack
  4. Plugin URI:
  5. Description: Modifies the behaviour of Events Manager plugin
  6.   Add the start event date in the permalink using the format /YYYY/mm/dd
  7. Author: Frédéric GILLES
  8. Version: 1.2
  9. Change Log:
  10.   1.1: Enable the event permalink everywhere in WordPress using the post_type_link filter hook
  11.   1.2: Fix the double dates in the permalink
  12. */
  13.  
  14. /**
  15.  * Add the start event date in the permalink
  16.  *
  17.  * @param string $permalink Permalink
  18.  * @param object $post Post object
  19.  * @param boolean $leavename
  20.  * @return string Permalink
  21.  */
  22. function fg_events_manager_get_permalink($permalink, $post, $leavename = false) {
  23.     if ( $post->post_type == 'event' ) {
  24.         $event = em_get_event($post->ID, 'post_id');
  25.         $start_date = $event->event_start_date;
  26.         $start_date = str_replace('-', '/', $start_date);
  27.         $event_slug = get_site_option('dbem_cp_events_slug');
  28.         $permalink = str_replace($event_slug, $event_slug.'/'.$start_date, $permalink);
  29.         return $permalink;
  30.     }
  31.     else return $permalink;
  32. }
  33.  
  34. add_filter('post_type_link', 'fg_events_manager_get_permalink', 10, 3);
  35.  
  36. /**
  37.  * Add a rewrite rule to accept the date in the permalink
  38.  *
  39.  */
  40. function fg_events_manager_add_rules() {
  41.     $event_slug = get_site_option('dbem_cp_events_slug');
  42.     add_rewrite_rule($event_slug.'/\d+/\d+/\d+/(.+)$', 'index.php?post_type=event&name=$matches[1]', 'top'); // single event
  43.    
  44.     //flush_rewrite_rules(); // To remove
  45. }
  46.  
  47. add_action('init', 'fg_events_manager_add_rules', 9); // Must be run before events manager
  48.  
  49. /**
  50.  * Plugin activation
  51.  *
  52.  */
  53. function fg_events_manager_activate() {
  54.     fg_events_manager_add_rules();
  55.     flush_rewrite_rules();
  56. }
  57.  
  58. register_activation_hook( __FILE__, 'fg_events_manager_activate' );
  59.  
  60. /**
  61.  * Plugin deactivation
  62.  *
  63.  */
  64. function fg_events_manager_deactivate() {
  65.     flush_rewrite_rules();
  66. }
  67.  
  68. register_deactivation_hook( __FILE__, 'fg_events_manager_deactivate' );
  69. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement