Passing parameters to a custom page template using clean urls

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.

<?php

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();
    $wpCPTURLRewrite->init();

endif; // CPTURLRewrite

UPDATE

I found a simpler way to handle this.

http://example.com/pageid/333/rid/444/title/your-title-here/

This new way utilizes add_rewrite_tag with _ to run the query for a page’s ID without messing up the main query. In the example above, 333 would just modify the query to look for that post ID in pre_get_posts. You could just as easily modify template_redirect or template_include.

/**
 * Register a rewrite endpoint for the API.
 */
function prefix__init() {

    // add tags with `_` prefix to avoid screwing up query
    add_rewrite_tag( '%_pageid%', '(\d*)' );
    add_rewrite_tag( '%_rid%', '(\d*)' );
    add_rewrite_tag( '%_title%', '([a-zA-Z\d\-_+]+)' );

    // create URL rewrite
    add_rewrite_rule( '^pageid/(\d*)/rid/(\d*)/title/([a-zA-Z\d\-_+]+)?', 'index.php?_pageid=$matches[1]&_rid=$matches[2]&_title=$matches[3]', 'top' );

    // required once after rules added/changed
    // flush_rewrite_rules( true );
}

add_action( 'init', 'prefix__init' );

/**
 * Handle data (maybe) passed to the API endpoint.
 *
 * @param $query
 */
function prefix__pre_get_posts( $query ) {

    if ( isset( $query->query_vars[ '_pageid' ], $query->query_vars[ '_rid' ], $query->query_vars[ '_title' ] ) ) {

        // set the query to search for a page using `_pageid`
        $query->set( 'p', $query->query_vars[ '_pageid' ] );
    }
}

add_action( 'pre_get_posts', 'prefix__pre_get_posts' );

To reference all the params passed later:

/**
* Debug the query params at any point
*/
function prefix__show_query_args() {
 global $wp_query;

    echo "<pre>";
    print_r( array (
        'pageid' => $wp_query->query_vars[ '_pageid' ],
        'rid'    => $wp_query->query_vars[ '_rid' ],
        'title'  => $wp_query->query_vars[ '_title' ],
        'file'   => __FILE__,
        'line'   => __LINE__,
    ) );
    echo "</pre>";
}

REFERENCE

Leave a Comment