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

For example:

site.com/contact (or site.com/?page=contact if not using permalinks)

when someone goes to that page I want to load a theme template, like page-contact.php

But I don't want to create a page in the dashboard for this work. How can I do this internally, within functions.php ?

I know it's possible because that bbpress plugin does it somehow for the /users/ pages, which are not standard wordpress pages...

share|improve this question

This is the technique I'm using on a site right now. It involves registering a query var for "template", and registering an endpoint called /contact, which rewrites internally to ?template=contact. Then you just check for that query variable at the template_redirect hook, and if its present, include the page template you want to load and exit.

/* Add a query var for template select, and and endpoint that sets that query var */
add_action( 'init', 'wpse22543_rewrite_system' );

function wpse22543_rewrite_system() {
    global $wp, $wp_rewrite;
    $wp->add_query_var( 'template' );   
    add_rewrite_endpoint( 'contact', EP_ROOT );
    $wp_rewrite->add_rule(  '^/contact/?$', 
                            'index.php?template=contact', 'bottom' );

/* Handle template redirect according the template being queried. */
add_action( 'template_redirect', 'wpse22543_select_template' );

function wpse22543_select_template() {
    global $wp;
    $template = $wp->query_vars;

    if ( array_key_exists( 'template', $template ) && 
        'contact' == $template['template'] ) {
        /* Make sure to set the 404 flag to false, and redirect 
               to the contact page template. */
        global $wp_query;
        $wp_query->set( 'is_404', false );
        include( get_stylesheet_directory().'/contact.php' );

This will serve up your contact.php file for any requests for ?template=contact or /contact.

There's probably a quicker way of doing it, but this works. Here's a more thorough tutorial: Create Fake Pages in WordPress.

share|improve this answer

You can do this just as you said. It is built into the template hierarchy of Wordpress since 2.9.

If you have a page named Contact, create a page template called page-contact.php which is different than page.php. And voila, it works.

From WP documentation.

Templates by page-ID or page-Slug

You can use different archives "page", with ID or slug in your name. Only Available with Version 2.9.



Is this what you wanted?

share|improve this answer
You could then put anything you want into that page-contact.php, including static HTML. – Pippin Jul 10 '11 at 15:34

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.