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.