Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

When a user browses to a URI beginning with "/forums", WordPress should call a specified template. For example, all of these URLs:

  • mysite.com/forums
  • mysite.com/forums/questions
  • mysite.com/forums/questions/1/my-question
  • mysite.com/forums/questions/ask
  • mysite.com/forums/users

... should call the following template: mytheme/page-forums.php

I believe this involves WP_Rewrite, but I have no clue what to do from here.

Any ideas?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

I've included some code that I use whenever I need to make additions to the Wordpress rewrite rules. It injects additional rules into the normal Wordpress rewrite logic so you can direct Wordpress to specific template files based on the URL. You can modify it to your needs, by adding more rules to the create_rewrite_rules() function, and additional query_vars to the add_query_vars() function.

<?php // Forums Class //
$ForumsCode = new Forums();
register_activation_hook( __file__, array($ForumsCode, 'activate') );

add_filter('rewrite_rules_array', array($ForumsCode, 'create_rewrite_rules'));
add_filter('query_vars',array($ForumsCode, 'add_query_vars'));

add_filter('admin_init', array($ForumsCode, 'flush_rewrite_rules'));
add_filter('template_include', array($ForumsCode, 'template_redirect_intercept'));

class Forums {

     function activate() {
        global $wp_rewrite;
        $this->flush_rewrite_rules();
    }

    function create_rewrite_rules($rules) {
        global $wp_rewrite;
        $newRule = array('forums/(.+)' => 'index.php?forumdata='.$wp_rewrite->preg_index(1));
        $newRule2 = array('forums/questions/(.+)' => 'index.php?questions=true&forumdata='.$wp_rewrite->preg_index(1));
        $newRules = $newRule + $newRule2 + $rules;
        return $newRules;
    }

    function add_query_vars($qvars) {
        $qvars[] = 'forumdata';
        return $qvars;
    }

    function flush_rewrite_rules() {
        global $wp_rewrite;
        $wp_rewrite->flush_rules();
    }

    function template_redirect_intercept($template) {
        global $wp_query;
        if ($wp_query->get('forumdata')) {
            $template = get_bloginfo('template_url') . '/page-forums.php';
        }
        return $template;
    }

    function pushoutput($message) {
        $this->output($message);
    }

    function output( $output ) {
        header( 'Cache-Control: no-cache, must-revalidate' );
        header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
        echo json_encode( $output );
    }
}
share|improve this answer
    
Thanks! The "template_include" filter was especially helpful. –  Matt Sep 20 '12 at 14:46
add comment

Your Answer

 
discard

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.