Support » Developing with WordPress » loading page for custom route

  • Hi

    I am creating a new plugin. I want to define some custom routes and load my code based on requested route. I defined a route with following code in main file of my plugin:

    
    register_activation_hook( __FILE__, 'cuibbp_activate');
    function cuibbp_activate() {
        cuibbp_add_rules();
        flush_rewrite_rules();
    }
    
    // Flush when deactivated
    register_deactivation_hook( __FILE__, 'cuibbp_deactivate' );
    function cuibbp_deactivate() {
        flush_rewrite_rules();
    }
    
    // Add the rewrite rule
    add_action( 'init', 'cuibbp_add_rules' );
    function cuibbp_add_rules() {
    add_rewrite_tag( '%stores%', '([^&]+)' );
        add_rewrite_rule( 'stores/?([^/]*)',
            'index.php?storepage=stores&store_id=$matches[1]', 'top' );
    }
    
    add_filter( 'query_vars', 'cuibbp_add_query_var' );
    function cuibbp_add_query_var( $vars ) {
        $vars[] = 'store_id';
        return $vars;
    }
    if(!is_admin())
    {
    $store_id = $_GET['store_id'];
    if(!empty($store_id))
    require_once( plugin_dir_path(__FILE__).'store.php');
    }
    
    

    I have store.php file in my plugin. When I try to access MY_SITE/stores or MY_SITE/stores/12, wordpress redirects to home page. I want to know how can I load a template file when user sends a request for new custom route in my plugin?

    thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    You can use the ‘template_include’ filter to specify a specific file be used as a template. It ought to be clear that you should only return your template file reference to the filter when the appropriate store values are in $_GET.

    You also need code to actually query for the requested store data. I’m assuming you’ve done this on your template file. I only mention this in case you haven’t done so.

    FWIW, “route” is a poor choice of terminology IMO for your question because “route” in WP is specifically for REST API routes. While your permalink, permastruct, request, URL, URI, or whatever it’s called as long as it’s not “route”, has a similar format to API routes, it doesn’t work in the API, so IMO it should not be called a “route”. If your intent was to make it an actual API route, you’re going about it the wrong way.

    Thread Starter Mostafa Shahiri

    (@mostafadeveloper)

    @bcworkz thanks for your reply. You are right. “route” is not a proper term for my question. I just want to define a custom URL structure for links in my plugins. When user clicks on the link, I want to display proper page. For example, in Woocommerce plugin file structure, different pages have been defined in templates directory, but I don’t know (I didn’t find the code) how does Woocommerce load the proper template page based on related request? I want to learn more about loading template files for different URLs in plugins.

    Thanks

    Moderator bcworkz

    (@bcworkz)

    Core code determines the template to use in template-loader.php. The source code there is fairly clear, have a look. I imagine WooCommerce uses the “template_include” filter (at line 77) in a similar manner. For WooCommerce specific information, you could try to examine the source code, but admittedly it’s hard to follow. You could try asking in their dedicated support forum. The expert users and devs there are in a better place to answer WC specific questions.

    You can see from the template-loader.php code that the template loaded depends on which query vars are set, determined by functions like is_front_page(). You might wonder how these query vars are determined from any particular request. That answer gets rather involved. For that, I refer you to the Query Overview.

    Thread Starter Mostafa Shahiri

    (@mostafadeveloper)

    @bcworkz Thanks for your explanation, I changed my code and I used template_include filter to load my template file, but it still redirects to home page. I don’t know why! I searched more for similar questions. All answers say using template_include filter can solve this problem. It seems this solution works for all but me!

    Moderator bcworkz

    (@bcworkz)

    Huh, that’s odd. AFAIK, everything goes through template loader. Add your callback function with a large priority argument to ensure it has the final say in template selection. Check the action is calling your added function by inserting an error_log() call. If no message is logged, some other code is redirecting before template loader. If a message is logged, verify your callback code is correctly deciding when to return your template and when to pass the default on through.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘loading page for custom route’ is closed to new replies.