• I have a database table containing a set of music bands. Right now I have a page template ‘viewband’ set up that takes a query string variable ‘id’ and uses it to pull all the info about that band for diaplsy. The URL looks like this:

    https://www.mysite.com/viewband/?id=34

    I want to create rewrite rules so that the URL can look like this:

    https://www.mysite.com/viewband/the-band-name/

    I was thinking it could be done using $wp_rewrite. When a new band is added to the database, I could add a new rewrite rule for that band.
    Here is my current code that runs when a new band is added to the table:

    function addRewriteRule($slug, $id) {
    	$rewriteSlug = $slug;
    	$rewriteID = $id;
    	add_filter('page_rewrite_rules','rewriteBandPages');
    	flushRules();
    }
    
    function rewriteBandPages($rules) {
    	global $rewriteSlug;
    	global $rewriteID;
    	$newrules = array();
    	$newrules['/viewband/('.$rewriteSlug.')$'] = 'id='.$rewriteID;
    	return $newrules + $rules;
    
    }
    
    function flushRules() {
    	global $wp_rewrite;
    	$wp_rewrite->flush_rules();
    }

    This isn’t doing anything, however. I get a 404 error when using the URL I wanted to create with these functions.

    Can anyone help me?

  • The topic ‘Custom Rewrite rules generated dynamically’ is closed to new replies.