• I want to make a plugin that display a template page whenever a user goes to page like this https://domainname.com/games/<gametitle&gt;
    (game title being replaced with any word and it’ll automatically redirect to the template page.

    I know how to make a plugin but after 5 hours messing about with permalinks I still am no further at figuring this out, does anyone have any advice on how to do this? or other plugins to look at for ideas?

    Cheers

Viewing 2 replies - 1 through 2 (of 2 total)
  • It can be a bit tricky at first. My sitemap generator plugin uses permalinks to handle pages of the sitemap.

    Here is some stripped down code from the plugin:

    function yyyyyyy_permalinks($rules) {
    	global $wp_rewrite;
    	if ($wp_rewrite->use_verbose_rules || !isset($wp_rewrite->use_verbose_rules)) {
    		$match_form = '$1';
    	} else {
    		$match_form = '$matches[1]';
    	}
    	$newrules['xxxxxxx/([0-9]{1,})/?$'] = 'index.php?&pagename=xxxxxxx&pg=' . $match_form;
    	$newrules = array_merge($newrules,$rules);
    	return $newrules;
    } 
    
    function yyyyyyy_query_vars ( $vars ) {
    	$vars[] = "pg";
    	return $vars;
    }
    
    add_filter('query_vars', 'yyyyyyy_query_vars');
    add_filter('rewrite_rules_array', 'yyyyyyy_permalinks');

    Notes:

    1) replace yyyyyyy with some unique name so the functions do not conflict with anything else

    2) replace xxxxxxx with the slug of the page you are checking for the query on.

    The first function creates the rules themselves. In this case, it checks for an integer in this format:

    https://yoursite.com/xxxxxxx/123

    And it translates to this in the back-end

    https://yoursite.com/index.php?&pagename=xxxxxxx&pg=123

    That is the standard way WP access pages, with the addition of the pg variable, which gets added in the second function.

    The second function basically registeres the new pg query var. Otherwise WordPress will ignore it.

    It is a starting point for you at least – you will just have to make it fit your needs ??

    I would just like to say thank you Dagon Design. Your post helped me to solve a problem I’d been having for days!

    The key for anyone who isn’t too sure about the above is to pass the entire slug as the pagename variable in the rewrite rule. Not just the page name itself.

    R.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to do pretty permalink rewrite rules in a plugin?’ is closed to new replies.