• Resolved nitrok

    (@nitrok)


    Hi!
    Firstly thank you for the excellent plugin!

    I have a problem with rewriting my URLs for viewing the images filtered by tag.
    I don’t see any image showing up when visiting my page via rewrited url (https://domain.com/photos-by-tag/tag-name). It works fine when visiting the page with the original url (https://domain.com/photos-by-tag/?tag=tag-name&current_item=tag-name)

    Is there any way I can make this work?

    Here are the rewrite functions from themes functions.php:

    // Register vars
    function register_query_vars( $vars ) {
        $vars[] = 'tag';
        $vars[] = 'current_item';
        return $vars;
    }
    add_filter( 'query_vars', 'register_query_vars' );
    
    // Rewrite tag-page URLs
    function tag_rewrite_rule() {
        add_rewrite_rule(
            '^photos-by-tag/([^/]*)/?',
            'index.php?page_id=2&tag=$matches[1]&current_item=$matches[1]',
            'top'
        );
    }
    add_action( 'init', 'tag_rewrite_rule' );

    Here’s also my shortcodes for the tag page:

    [mla_tag_cloud taxonomy=attachment_tag limit=100 smallest=0.8 largest=1.2 unit=em minimum=1 single_text="%d Photo" multiple_text="%d Photos" separator="" mla_link_href="{+page_url+}?tag={+slug+}&current_item={+slug+}" current_item="{+request:current_item+}" mla_item_value="{+slug+}"]
    
    [mla_gallery columns=0 mla_style="none" size="medium" my_custom_sql="attachment_tag='{+request:tag+}' orderby=date order=DESC"  posts_per_page=7 mla_caption=" "]
    
    [mla_gallery my_custom_sql="attachment_tag='{+request:tag+}'" posts_per_page=7 mla_output="paginate_links,prev_next"]
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author David Lingren

    (@dglingren)

    Thanks for the positive MLA feedback and for an interesting question. Thanks as well for including the complete text of your shortcodes and the PHP code for your rule; very helpful. I haven’t used the WordPress rewrite rules so it took some tinkering to get a solution.

    I was able to simplify your shortcodes, eliminating the tag= parameter since it’s always the same as current_item=. I also changed the URLs in the tag cloud to match your rewrite structure. Here are the shortcodes I ended up using:

    [mla_tag_cloud taxonomy=attachment_tag limit=100 smallest=0.8 largest=1.2 unit=em minimum=1 single_text="%d Photo" multiple_text="%d Photos" separator="" mla_link_href="{+page_url+}{+slug+}/" current_item="{+request:current_item+}" mla_item_value="{+slug+}"]
    
    [mla_gallery columns=0 mla_style="none" size="medium" attachment_tag='{+request:current_item+}' orderby=date order=DESC  posts_per_page=7 mla_caption=" "]
    
    [mla_gallery attachment_tag='{+request:current_item+}' orderby=date order=DESC posts_per_page=7 mla_output="paginate_links,prev_next"]

    Note that I added the orderby=date order=DESC parameters to the pagination shortcode. The data selection parameters must be identical in both [mla_gallery] shortcodes to get consistent results.

    The WordPress Rewrite API is specialized. It does not work in the same way that redirect managers such as the Redirection plugin do. You can find a discussion of that approach in this earlier topic:

    Cleaning up URL queries in paginated gallery pages, started by @1ore

    The WordPress API does not use HTTP 301 redirections; it only affects the query variables used by the WordPress WP_Query class to match a URL to the right theme template file. It does not update the PHP $_REQUEST array with the custom parameters.

    Here is the rewrite code I added to my functions.php file for your application:

    // Rewrite tag-page URLs
    function tag_rewrite_rule() {
        add_rewrite_rule(
            'photos-by-tag/([^/]*)/?',
            'index.php?name=photos-by-tag&current_item=$matches[1]',
            'top'
        );
    }
    add_action( 'init', 'tag_rewrite_rule' );
    
    // Register vars
    function register_query_vars( $vars ) {
        $vars[] = 'current_item';
        return $vars;
    }
    add_filter( 'query_vars', 'register_query_vars' );
    
    // Copy current_item from query vars to $_REQUEST 
    function current_item_copy( $template ) {
    	$current_item = get_query_var( 'current_item' );
    
        if ( !empty( $current_item ) ) {
    		$_REQUEST['current_item'] = $current_item;
        }
    
        return $template;
    }
    add_filter( 'template_include', 'current_item_copy' );

    Note that the query parameter used in the rewrite rule must match the site’s Permalink structure setting. My site’s Permalink structure is set to “Post name”, so I must use the name=photos-by-tag query argument in the rewrite rule. Any other value, e.g., p= or page_id=2 will result in two queries and the custom query argument(s) are not passed along to the second query.

    I have added a third function, current_item_copy(), that copies the current_item value from the query arguments to the $_REQUEST array where MLA expects it. I adapted that from a User Contributed Note I found in the add_rewrite_rule() Codex documentation.

    I am marking this topic resolved, but please update it if you have any problems or further questions regarding the above solution. Thanks again for an interesting question and for your interest in the plugin.

    Thread Starter nitrok

    (@nitrok)

    Thank you for taking a look and offering a solution. It seems to be working well!

    • This reply was modified 2 months, 2 weeks ago by nitrok.
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.