Clean URLs for custom $_GET variables

You should look at “Rewrite Endpoints”, which are much simpler to work with than custom rewrites. And they fit your use case perfectly: http://codex.wordpress.org/Rewrite_API/add_rewrite_endpoint

You might add your “show” endpoint like so:

function wpsx_37961_add_endpoints() {

    // Add the "show" rewrite endpoint to all URLs
    add_rewrite_endpoint('show', EP_ALL);

}
add_action('init','wpsx_37961_add_endpoints');

Then you can check for its value on the WP action (or maybe earlier, I’m not sure), like:

function wpsx_37961_check_endpoint() {

    $show = $wp_query->get( 'show' );

    // Now, do whatever template switching you want based on the $show var's value

}
add_action('wp','wpsx_37961_check_endpoint');

The work of actually switching templates is up to you, but this gets your endpoints in place. No filtering query vars, no hacking together rewrite rules, no tough stuff 🙂

Leave a Comment

tech