Custom Fancy and SEO friendly Urls in WordPress

I have searched a lot for this wordpress tricks or hacks to make my own Custom Fancy and SEO Friendly Urls, mostly I failed. Tried by changing or modifying .htaccess RewriteRule method and with custom wordpress WP Rewrite API function add_rewrite_rule(). After googling with this new WP Rewrite API method I found this article “Custom rewrite rules in WordPress (add_rewrite_rule and add_rewrite_tag)” by Dan but some how that dint worked to me.

So thought there might be some other alternative solution to do this job, started scrolling through the support forms of wordpress, then I found a thread “Correct usage of add_rewrite in plugin” by bennybobw which gives an another alternative method to achieve this custom SEO friendly urls.

Custom Fancy and SEO friendly Urls in WordPress
Custom Fancy and SEO friendly Urls in WordPress


Here is the snippet which should be used either in your themes functions.php or into your custom wordpress plugin:

I tried the following snippet and I succeeded 🙂

function myvar_flush_rewrite() {
  global $wp_rewrite;
  $wp_rewrite->flush_rules();
}
function myvar_vars($public_query_vars) {
    $public_query_vars[] = 'myvar';
    return $public_query_vars;
}
function myvar_add_rewrite_rules($wp_rewrite) {
  $new_rules = array(
     'video-buzz/(.+)' => 'index.php?pagename=video-buzz&myvar=' . $wp_rewrite->preg_index(1));
  $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('init', 'myvar_flush_rewrite');
add_filter('query_vars', 'myvar_vars');
add_action('generate_rewrite_rules', 'myvar_add_rewrite_rules');

I have used the above method to show my Video Buzz custom queried pages in short custom, fancy and SEO friendly urls.

Let me explain you in detail:

  1. I have created a custom template
  2. The template has some dynamic content which displays based on the custom query “myvar” passed like “http://www.wittysparks.com/video-buzz/?myvar=sports”
  3. To make the above url as “http://www.wittysparks.com/video-buzz/sports/” I have used the above action and filters to make the custom fancy and short urls – which are SEO friendly
  4. You can grab the custom query “myvar” value in your custom template or plugin with get_query_var() function as “get_query_var(‘myvar’)
  5. We are done 🙂 – In this way you can have wordpress custom fancy and SEO friendly urls for your own custom plugin or template.

Just explore and let me know if you find and difficulties in implementing this, will try my best to help you.