0

I am writing a basic gallery plugin for WordPress. In the plugin users can mark images as favorites and there is a page where users can see their favorites.

I want to setup some rewrite rules for the galleries (domain.com/page/gallery/gallery-name) instead of query string variables (domain.com/page/?gallery=gallery-name)

In my plugin settings, users can select a page for the plugin to run off. I store the page ID and want to use this as a base for everything in the galleries.

Example URLs would be:

  • domain.com/page - This would show the list of all the galleries
  • domain.com/page/gallery/gallery-name - Gallery page with thumbnail images
  • domain.com/page/gallery/gallery-name/2 - Paginated
  • domain.com/page/image/image-name - Single image with various details
  • domain.com/page/favorites - Listing images marked as favorites

The add_rewrite_rule stuff is somehow completely confusing me and I can't get anything to work. I am using flush_rewrite_rules(), I know that is a common mistake when trying to setup custom rewrite rules.

EDIT: I made progress and here is what I have so far:

$home = get_post($home_id);
// Gallery
add_rewrite_rule($home->post_name."/gallery/([^/]+)/page/?([0-9]{1,})/?$",'index.php?p=4&sunshine_gallery=$matches[1]&paged=$matches[2]','top');  
add_rewrite_rule($home->post_name."/gallery/([^/]+)/?$",'index.php?p=4&sunshine_gallery=$matches[1]','top');  
add_rewrite_rule($home->post_name."/gallery/([^/]+)/?$",'index.php?pagename='.$home->post_name.'&sunshine_gallery=$matches[1]','top'); 
// Image
add_rewrite_rule($home->post_name."/image/([^/]+)/?$",'index.php?pagename='.$home->post_name.'&sunshine_image=$matches[1]','top'); 

However, the pagination rules are not catching and the 'paged' variable is always 0.

2
  • 1
    At least post the code you've tried.
    – s_ha_dum
    Commented Feb 25, 2013 at 5:00
  • I didn't have any, I was completely baffled. However, I was able to make some progress and have updated the question with my sample code.
    – sccr410
    Commented Feb 26, 2013 at 17:55

1 Answer 1

0

I finally got my rewrite rules to work here:

$home = get_post(Sunshine::$options['page']);
// Gallery
add_rewrite_rule($home->post_name."/gallery/([^/]+)/page/?([0-9]{1,})/?$",'index.php?pagename='.$home->post_name.'&sunshine_gallery=$matches[1]&paged=$matches[2]','top');  
add_rewrite_rule($home->post_name."/gallery/([^/]+)/?$",'index.php?p='.$home->post_name.'&sunshine_gallery=$matches[1]','top');  
add_rewrite_rule($home->post_name."/gallery/([^/]+)/?$",'index.php?pagename='.$home->post_name.'&sunshine_gallery=$matches[1]','top'); 
// Image
add_rewrite_rule($home->post_name."/image/([^/]+)/?$",'index.php?pagename='.$home->post_name.'&sunshine_image=$matches[1]','top'); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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