I have a third party events plugin that uses custom tables in the database. I have created a theme file (event.php) that pulls the data in based on a slug that is ideally given in the URL.

I want to create a custom URL rewrite rule for this template, so a URL like this:


Will call the event.php file, grabbing the specific event they have requested (the slug).

The class I am including in the functions.php to accomplish this is:

$EventpageCode = new Eventpage();
register_activation_hook( __file__, array($EventpageCode, 'activate') );

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

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

class Eventpage {

    function activate() {
        global $wp_rewrite;

    function create_rewrite_rules($rules) {
        global $wp_rewrite;
        $newRule = array('event/(.+)' => 'index.php?pagename=event&eventid='.$wp_rewrite->preg_index(1));
        $newRules = $newRule + $rules;
        return $newRules;

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

    function flush_rewrite_rules() {
        global $wp_rewrite;

    function template_redirect_intercept($template) {
        global $wp_query;
        if ($wp_query->get('eventid')) {
            $template = locate_template( array( 'event.php' ) );
        return $template;

    function pushoutput($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 );

This works great for displaying the content, except that the page throws a 404 error (but still displays the content). Any suggestions on how to avoid having the 404 error returned?


Just remove pagename=event& and reset your rewrite rules...

$newRule = array('event/(.+)' => 'index.php?eventid='.$wp_rewrite->preg_index(1));

*template_include* is a filter NOT an action!

add_filter('template_include', array($EventpageCode, 'template_redirect_intercept'));

You can use *wp_title* function or filter to modify your page title.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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