• Resolved davidnard

    (@davidnard)


    Hi Everyone,

    First off, my apologies if this has been asked and answered elsewhere; I couldn’t find an answer so hopefully some wordpress guru will know.

    I currently have a setup similar to:
    https://mysite.com/PAGENAME/SUBPAGE/?var1=foo&var2=bar

    Where PAGENAME and SUBPAGE are auto-generated by wordpress (actual pages in the system). The variables are tagged onto the end and processed by php code in a custom page template.

    What I’d like to do is change to something like:
    https://mysite.com/PAGENAME/SUBPAGE/foo/bar

    I haven’t had a ton of experience working with htaccess files, and I know wordpress does some stuff internally as well. I keep banging my head against the wall, any suggestions/links? I will drink a beer in your name if you can assist ??

    -Dave

Viewing 3 replies - 1 through 3 (of 3 total)
  • I’m game… have a beer… stay out of the htaccess file… keep your sanity… leave the wall alone…

    First, you can go ahead and switch your permalinks to the pretty ones. Then you’ll get /PAGENAME/SUBPAGE/ and you’re half way there.

    The key to the rest is the add_rewrite_endpoint function, it will do what you need (sort of… more like ?foo=bar… but you can take it from there). The code below is for PAGES, but you can change the second argument to turn it on for other items too.

    Add something like this to your theme’s functions.php or your plugin:

    // flush the rewrite rules
    function foobar_flush_rewrite_rules() {
    	global $wp_rewrite;
       	$wp_rewrite->flush_rules();
    }
    add_action('init', 'foobar_flush_rewrite_rules');
    
    // add foo as a possible "tail" item
    add_rewrite_endpoint('foo',EP_PAGES);
    
    // add foo as an allowed query var
    function foobar_variables($public_query_vars) {
    	$public_query_vars[] = 'foo';
    	return $public_query_vars;
    }
    add_filter('query_vars', 'foobar_variables');
    
    // OPTIONAL - sample filter action if the query var is seen -
    // I'm using it to toss in an extra stylesheet that changes the page's
    // appearance
    function foobar_add_stylesheet() {
    	// check for variants in link structure
    	if (get_query_var('foo')) {
    	    // add stylesheet to hide certain divs
    	    echo "\n".
                    '<style type="text/css" media="screen">@import "' .
                    get_bloginfo('stylesheet_directory') .
                    '/foo.css";</style>'."\n";
    	}
    }
    add_action('wp_head', 'foobar_add_stylesheet', 1);

    You can also use the get_query_var() function on template pages to deal with your parameters.

    I have no idea if this will work with more than one endpoint.

    Thread Starter davidnard

    (@davidnard)

    Awesome, thank you! I will try this out tonight and report back on how it works out for me.

    Thanks
    Dave

    Thread Starter davidnard

    (@davidnard)

    Worked like a charm, thanks a ton Roger!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Adding onto URL with custom Query?’ is closed to new replies.