0

I am using this example to rewrite url to load a given file car-details.php but I stil get error Page not found when I access domain.com/account/account_page/9 How can I get this working.

class Your_Class
{

    public function init()
    {
        add_filter( 'template_include', array( $this, 'include_template' ) );
        add_filter( 'init', array( $this, 'rewrite_rules' ) );
    }

    public function include_template( $template )
    {
        //try and get the query var we registered in our query_vars() function
        $account_page = get_query_var( 'account_page' );

        //if the query var has data, we must be on the right page, load our custom template
        if ( $account_page ) {

            return CUSTOMER_CAR_PLUGIN_DIR.'pages/customer-car-details.php';
        }

        return $template;
    }

    public function flush_rules()
    {
        $this->rewrite_rules();

        flush_rewrite_rules();
    }

    public function rewrite_rules()
    {
        add_rewrite_rule( 'account/(.+?)/?$', 'index.php?account_page=$matches[1]', 'top');
        add_rewrite_tag( '%account_page%', '([^&]+)' );
    }

}

add_action( 'plugins_loaded', array( new Your_Class, 'init' ) );

// One time activation functions
register_activation_hook( CUSTOMER_CAR_PLUGIN_DIR, array( new Your_Class, 'flush_rules' ) );
}
  • Could you please try this domain.com/account/9 instead of this domain.com/account/account_page/9? account_page is your query variable it doesn't have to be in the query string. – obiPlabon Feb 18 '18 at 6:31
  • same issue page not found.I have changed to CUSTOMER_CAR_PLUGIN_DIR.'pages/customerxxx.php'; which doesn't exist in the plugin dir and could not throw error file not found. I am loading a custom file from my plugin ,could that be the issue then? – ovicko Feb 18 '18 at 6:53
0

Instead of adding query_vars with add_rewrite_tag(), use the query_vars filter hook. I've used the following code to test your routing and it's working just fine.

class OP_Plugin {

    public function init() {
        add_action( 'init', array( $this, 'add_rewrite_rules' ) );
        add_filter( 'query_vars', array( $this, 'add_query_vars' ) );
        add_filter( 'template_include', array( $this, 'add_template' ) );
    }

    public function add_template( $template ) {
        $account_page = get_query_var( 'account_page' );
        if ( $account_page ) {
            echo "Working! \n";
            echo "Query: {$account_page}";
            // return CUSTOMER_CAR_PLUGIN_DIR.'pages/customer-car-details.php';
            return '';
        }
        return $template;
    }

    public function flush_rules() {
        $this->rewrite_rules();
        flush_rewrite_rules();
    }

    public function add_rewrite_rules() {
        add_rewrite_rule( 'account/(.+?)/?$', 'index.php?account_page=$matches[1]', 'top' );
    }

    public function add_query_vars( $vars ) {
        $vars[] = 'account_page';
        return $vars;
    }

}
$op_plugin = new OP_Plugin();
$op_plugin->init();
|improve this answer|||||
  • This does not work for me. I think you meant $this->add_rewrite_rules();, but even with that it doesn't work, at least not for me on WP 5.3 with nginx. – Chris Nov 23 '19 at 21:27
  • Flush your permalink settings then try – obiPlabon Nov 25 '19 at 20:36
  • Yeah, I realized I had to do that after playing with it a bit. I was a bit confused by your flush_rules function, which actually isn't used. Also, this doesn't work with the OP's original routing (domain.com/account/account_page/9) - it returns "account_age/9", but like you mentioned in your comment above, it's better to use a route like domain.com/account/9, which does work. – Chris Nov 26 '19 at 18:37

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.