WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am now passing parameters to a custom template in the following format


I have created two tables . So I need to fetch data and display them on those pages.

I would like it to be using clean URL format like:


Can anyone tell how this can be achieved?


share|improve this question
Totally possible, but you have to be mindful of your variable slugs to avoid conflicts. Take a look at this article on the WordPress codex... this is probably the hook you would want to use and it shows a sample rewrite array... codex.wordpress.org/Plugin_API/Filter_Reference/… – Matt van Andel Mar 3 '13 at 16:27

If you want a simple solution this might be it (haven't tested the code so might not be so simple if it's not working...) or atleast it give some hint what you need to do. If you visit http://thereforei.am/2011/10/28/advanced-taxonomy-queries-with-pretty-urls/ you will find a bonus prize: A Rewrite Rule Generator in the article. I have used this before but not in a situation exactly like this. Try to combine that function with something like this:


/* Generate rewrite rules. */
add_action( 'generate_rewrite_rules', 'example_add_rewrite_rules' );
function example_add_rewrite_rules() {
    global $wp_rewrite;
    $new_rules = eg_generate_rewrite_rules( 'post' , array('pageid','rid','title')); //    post = the post type you use
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;

/* Flush rewrite rules on theme activation only. */
add_action( 'load-themes.php', 'frosty_flush_rewrite_rules' );
function frosty_flush_rewrite_rules() {
    global $pagenow;
    if ( 'themes.php' == $pagenow && isset( $_GET['activated'] ) )

If you don't want to use the eg_generate_rewrite_rules function, you need to set up the rules yourself. You can find information on how to do that in that article.

More info: http://codex.wordpress.org/Rewrite_API/add_rewrite_tag

share|improve this answer

add_rewrite_rule() allows you to turn the pretty url into variables.

  • numbers: (\d*)
  • section: /rid/ or /pageid/
  • slug: ([a-zA-Z0-9-]+

Here is a class to register the rewrite and handle the request if a match has been found.


if ( ! class_exists( 'CPTURLRewrite' ) ):

    class CPTURLRewrite {
        const ENDPOINT_QUERY_NAME  = 'pageid';
        const ENDPOINT_QUERY_PARAM = '__pageid';

        // WordPress hooks

        public function init() {
            add_filter( 'query_vars', array ( $this, 'add_query_vars' ), 0 );
            add_action( 'parse_request', array ( $this, 'sniff_requests' ), 0 );
            add_action( 'init', array ( $this, 'add_endpoint' ), 0 );

        // Add public query vars

        public function add_query_vars( $vars ) {

            // add all the things we know we'll use

            $vars[] = static::ENDPOINT_QUERY_PARAM;
            $vars[] = 'pageid';
            $vars[] = 'rid';
            $vars[] = 'title';

            return $vars;

        // Add API Endpoint

        public function add_endpoint() {

            // numbers:   (\d*)
            // section:   /rid/
            // slug:      ([a-zA-Z0-9-]+

            add_rewrite_rule( '^' . static::ENDPOINT_QUERY_NAME . '/(\d*)/rid/(\d*)/title/([a-zA-Z0-9-]+)?', 'index.php?' . static::ENDPOINT_QUERY_PARAM . '=1&pageid=$matches[1]&rid=$matches[2]&title=$matches[3]', 'top' );

            flush_rewrite_rules( false ); //// <---------- REMOVE THIS WHEN DONE

        // Sniff Requests

        public function sniff_requests( $wp_query ) {
            global $wp;

            if ( isset(
                $wp->query_vars[ static::ENDPOINT_QUERY_PARAM ],
                $wp->query_vars[ 'pageid' ],
                $wp->query_vars[ 'rid' ],
                $wp->query_vars[ 'title' ] ) ) {
                $this->handle_request(); // handle it

        // Handle Requests

        protected function handle_request() {
            global $wp;

            // (optional) act on the query vars

            $pageid = $wp->query_vars[ 'pageid' ];
            $rid = $wp->query_vars[ 'rid' ];
            $title = $wp->query_vars[ 'title' ];

            // (optional) select your custom template

            add_filter( 'template_include', function( $original_template ) {
                return __DIR__ . '/custom.php';
            } );

    $wpCPTURLRewrite = new CPTURLRewrite();

endif; // CPTURLRewrite
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.