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

i set my wordpress permlink to /%category%/%postname% now i'm using Nextgen Gallery so when i have a gallery in a page it will be something like /cat/page?gallery=10 i want to make it something like cat/page/gallery id/ i tried alot with wp_rewrite ... but no luck ... any help ?

this is what i'm testing on .. it's not my code it's provided from wordpress ... i will put everything in its place when i found the way ... so don't worry i will not flushRules on init ... thanks a lot


// Remember to flush_rules() when adding rules
function flushRules(){
 global $wp_rewrite;

// Adding a new rule
function wp_insertMyRewriteRules($rules)
 $newrules = array();

    //i don't know what should i put here
 $newrules[''] = '';
 return $newrules + $rules;

// Adding the id var so that WP recognizes it
function wp_insertMyRewriteQueryVars($vars)
    array_push($vars, 'id');
    return $vars;
share|improve this question

Have you had a look at the rewrite API? Maybe this can solve your problem?
If you use the rewrite API, then you probably should turn of NextGen rewrite function.

share|improve this answer

With an extra endpoint

Since you want to add something to the end of every post/page rewrite rule, you probably can just add a rewrite endpoint. These are regexes of the form /[endpoint_name](/[optional_extra_stuff])? that are appended to the already generated rules for pages, posts, archives, ...

You define on which structures you want to add them by setting the endpoint mask. This is a bitmask, so you can combine different groups using the | operator, like this: EP_PERMALINK | EP_PAGES will match for every page and every permalink (full post and date-based archives). The default list of endpoints can be found at the top of wp-includes/rewrite.php.

The following code will add /gallery(/(.*))? to the existing rewrite rules for pages, posts and date-based archives (for some reason they are generated twice, once in EP_PERMALINK and once in EP_DATE):

add_filter( 'init', 'wpse4498_init' );
function wpse4498_init()
    add_rewrite_endpoint( 'gallery', EP_PERMALINK | EP_PAGES );

With explicit new rewrite rules

You can also do is explicitly, which might give you more control over the generated rules.

The rewrite rule itself it quite easy: take the generic post rule (.+?)/([^/]+) (category/pagename) and add your gallery structure. NextGEN always adds its query vars, not only when permalinks are enabled, so we can just use the gallery var:

'(.+?)/([^/]+)/gallery/([0-9]{1,})/?$' =>

The placement of your extra rewrite rule is tricky. Since your permalink structure is /%category%/%postname%/ you have verbose rewrite rules, and can't put a generic (.+?)/([^/]+) at the top: your pages will stop working. You can't put it at the bottom, as the attachment rules are quite generic and will have matched the URL before it reaches your rule. I think the best place to add it is with the post_rewrite_rules filter. This will give the following:

add_filter( 'post_rewrite_rules', 'wpse4498_post_rewrite_rules' );
function wpse4498_post_rewrite_rules( $post_rewrite_rules )
    $post_rewrite_rules = array(
        '(.+?)/([^/]+)/gallery/([0-9]{1,})/?$' =>
    ) + $post_rewrite_rules;
    return $post_rewrite_rules;

This only covers posts, if you also want to support pages with galleries it will become much more complicated since you have to add them for each verbose page group.

share|improve this answer

Have you activated the permalinks in the NextGen settings? Under Gallery -> Options -> General select 'activate permalinks', then reset your general permalink settings once. It's not the cleanest URL in the world (it grabs the page ID instead of the name) but it does the job.

share|improve this answer
yes i did that, but i'm not happy with the ID thing ... plus the project description the page URL should be cat/page/gallery name/ is that possible ? Thanks a lot for your answer .. – ra.htial Nov 25 '10 at 0:08
Probably not without hacking the plugin itself, since it creates the rewrite rules. the other option would be to create pages with the hierarchy you want, then place each gallery individually. – Norcross Nov 25 '10 at 4:43
hmmmmmm ... but i edited the plugin so every image has it's own page ... i can't create a page for every image ... plus my website content is dynamic – ra.htial Nov 25 '10 at 11:20
sorry if I wasn't clear. not a page for each image, rather, a page for each gallery. – Norcross Nov 27 '10 at 0:40

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.