Fuller Web Development

  BLOG | CONTACT | CLIENTS

WordPress URL Rewrite

WordPress has it’s own URL management system, and when developing websites it’s convenient to be able to add new rules dynamically. I’ve used this for two websites, music.vtechphones.com and for a site I am building now. It’s also great to keep all modifications in one place, so upgrades are easier, and easier for another developer to pick up!

Create a new plugin, and you place your rules into it doing something similar to the PHP below:


add_filter('rewrite_rules_array','wp_insert_my_rewrite_rules');
add_filter('init','flush_rules');

// Remember to flush_rules() when adding rules
function flush_rules(){
  global $wp_rewrite;
  $wp_rewrite->flush_rules();
}

// Adding a new rule
function wp_insert_my_rewrite_rules($rules){
  $newrules = array();

  $newrules['(articles)$'] = 'index.php?post_type=article';
  $newrules['(gallery)$'] = 'index.php?post_type=gallery';
  $newrules['(projects)$'] = 'index.php?post_type=project';
  $newrules['(blog)$'] = 'index.php?post_type=post';

  return $newrules + $rules;
}