4

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', 'my_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

1
  • What $_GET or $_REQUEST args are you looking for? Once WP parses the URL and creates the query, you should be looking in $wp_query->query_vars instead ...
    – EAMann
    Feb 1, 2012 at 20:36

1 Answer 1

5

You can use the function get_query_var().

Like so:

$event_slug = get_query_var('event_slug');

More info in the Codex.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.