How to create SEF URL’s in WordPress

While working on one of my client’s plugin I needed to provide search engine friendly url’s for some custom functionality that was needed and existing plugin was not supporting such urls

but for search engine freindly url’s I want it to be like

So here I am sharing small code snippet which works perfectly for my plguin and showing nice url’s.

We need to update the theme function.php file, just add the below code in function.php file and modify variables according to your need.


// Remember to flush_rules() when adding rules
function flushRules()
  global $wp_rewrite;

// Adding a new rule
function wp_insertRewriteRules($rules)
  $newrules = array();
  $newrules['(mytestpage)/([a-zA-Z0-9_-]*)/([a-zA-Z0-9_-]*)$'] =
  $allrules = $newrules + $rules;
  return $allrules;

// Adding the variables  so that WP recognizes it
function wp_insertQueryVars($vars)
 array_push($vars, 'id');
 array_push($vars, 'var1');
 array_push($vars, 'var2');
 return $vars;

To access these variables from plugin file use following code:

 $var1 = $wp_query->query_vars['var1'];
 $var2 = $wp_query->query_vars['var2'];

Now this will solve your plugins search engine friendly issues.