WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The following is a snippet from single.php

                                    <?php endif; ?>
                                    if($_GET['m'] != "gallery") {
                                    else {
                                            echo do_shortcode('[gallery link="file"]');

                                    <?php wp_link_pages(); ?>

                                    <div class='clear'></div>

So if I append ?m=gallery to the URL it will convert single.php into a gallery, else it will show the normal content. I was wondering how I could rewrite ?m=gallery to just /gallery. I am using /%category%/%postname%/ as a custom permalink structure. I would like /%category%/%postname%/%m% where %m% is $_GET['m']. My htaccess has the following:

    RewriteEngine On

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    # END WordPress

Any help is appreciated. Thanks.

share|improve this question
up vote 2 down vote accepted

Your best bet is to use the WordPress rewrite API. No .htaccess required.

First, hook into init and add a rewrite endpoint to your permalinks. This tells WordPress that whens some visits /category/post-slug/gallery match the new endpoint rewrite. it also takes care of adding the query variable for you so you don't have to do that.

add_action( 'init', 'wpse27638_add_rewrite' );
function wpse27638_add_rewrite()
    add_rewrite_endpoint( 'gallery', EP_PERMALINK );    

Next up we have to hack the endpoint system a bit because it doesn't automatically work whens someone visits some-permalink/some-endpoint: your new query variable (gallery) would only contain a value when someone visits category/permalink/gallery/something. No good. So we hook into request. If our new gallery query variable is set, we'll just make it so the value is always set to true.

add_filter( 'request', 'wpse27638_request' );
function wpse27638_request( $vars )
    if( isset( $vars['gallery'] ) ) $vars['gallery'] = true;
    return $vars;   

Next up, we need to hook into the_content. If our new query var is set, we'll return a gallery shortcode in place of the content. Otherwise, we'll just return the content. You can remove your if else statement from single.php and just put in the_content in its place.

add_filter( 'the_content', 'wpse27638_content_filter' );
function wpse27638_content_filter( $content )
    if( ! is_singular() ) return $content;
    if( get_query_var( 'gallery' ) )
        return '[gallery link="file"]'; 
        return $content;        

Only one finishing touch on our plugin. Add the new endpoint and flush the rewrite rules on activation. And flush the rewrite rules again on deactivation.

register_activation_hook( __FILE__, 'wpse27638_activation' );
function wpse27638_activation()

register_deactivation_hook( __FILE__, 'wpse27638_deactivation' );
function wpse27638_deactivation()

Here's the only thing as a plugin (ready to use): https://gist.github.com/1191865

share|improve this answer
Thank you so much! How does this scale? Does it add many more queries? – v0idless Sep 4 '11 at 3:35
Not sure. You're going to have to ask someone more skilled in the ways of server stuff/optimization than I. – chrisguitarguy Sep 4 '11 at 3:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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