Skip to main content
1 of 2

add_rewrite_rule not producing anything in $_GET

Trying to get pretty permalinks happening for a plugin that displays events

I have this:

function my_add_rewrite_rules() {

  global $wpdb;
  $org_options = get_option('event_settings');      
  // get id for events page     
  $event_page_id = $org_options['event_page_id'];   

  $SQL = 'SELECT post_name  FROM wp_posts WHERE ID = %d';
  $event_page = $wpdb->get_var( $wpdb->prepare( $SQL, $event_page_id ));

  add_rewrite_rule( $event_page . '/([^/]+)?/$', 'index.php?pagename=' . $event_page . '&event_slug=$matches[1]', 'top' );
  add_rewrite_rule( $event_page . '/([^/]+)?$', 'index.php?pagename=' . $event_page . '&event_slug=$matches[1]', 'top' ); 
                                                    
}
add_action( 'init', 'add_rewrite_rules' );

and this:

function add_query_vars( $query_vars ) {
  $query_vars[] = 'event_slug';
  return $query_vars;
}
add_filter( 'query_vars', 'add_query_vars' );   

the rewrite rules get flushed in the admin when the 'event_settings' options are updated.

I am using an amazing plugin by Jan Fabry called "Monkeyman Rewrite Analyzer" which can show you how your rewrite rules are being processed (check it out it's great).

This is the results from the above:

Test URL
    http://localhost/events/some-event

Pattern
    events/([^/]+)?$    

Substitution 
    pagename: events
    event_slug: some-event

also appears to work with an additional trailing slash:

Test URL
    http://localhost/events/some-event/

Pattern
    events/([^/]+)?/$   

Substitution 
    pagename: events
    event_slug: some-event

these results appear to be working properly, but when I enter "http://localhost/events/some-event" into my browser, it does not redirect as it should.

The resulting $_GET and $_REQUEST arrays are empty.

What am I doing wrong???

Any and all help is greatly appreciated, but correct answers will be appreciated more ; )

Thanks