WP-Router icon indicating copy to clipboard operation
WP-Router copied to clipboard

add is_route function

Open vwasteels opened this issue 12 years ago • 8 comments

Hello,

Is there a way to test what route I'm showing,

example, in my header.php, I'd like to do :

if(is_route('my_custom_routing')) { echo 'menu-active'; }

thanks

vwasteels avatar Jun 04 '13 15:06 vwasteels

$router = WP_Router::get_instance();
if ( $router->get_route('my_custom_routing') !== NULL ) { echo 'menu-active'; }

But you're right, there should be a template tag for that.

jbrinley avatar Aug 08 '13 15:08 jbrinley

I tried using your method as

$router = WP_Router::get_instance();
if ( $router->get_route('help-route-id') !== NULL ) {  echo 'Help section'; }

but "Help section" is output on to every page, not only the only ones routed through "help-route-id". This happened when placing the above code in either header.php or page.php.

Is there any other way to check the current route?

P.s. thanks for a great plugin :+1:

jonjhiggins avatar Aug 16 '13 12:08 jonjhiggins

Hello,

I ended up doing like this :

// in header.php :
if(get_query_var('is_route_wiki') == 'true') echo 'menu-active';

// in functions.php :
$route_args = array(
                  'path'            => '^wiki',
                  'query_vars'      => array(
                    'is_route_wiki' => 'true'  // ***** this is the line ***
                    ),
                  'page_callback'   => 'empty_callback',
                  'page_arguments'  => array(),
                  'access_callback' => true,
                  'title'           => 'Wiki',
                  'template'        => array(
                    'templates/_wiki.php',
                    TEMPLATEPATH . 'templates/_wiki.php'
                )
              );
$router->add_route( 'wiki', $route_args );

vwasteels avatar Aug 16 '13 13:08 vwasteels

Aah brilliant - that works for me! And thanks for the quick reply

jonjhiggins avatar Aug 16 '13 13:08 jonjhiggins

you're welcome !

vwasteels avatar Aug 16 '13 15:08 vwasteels

@jbrinley : do you plan adding an is_route('my-route'); template tag ? Just to know if I still need to use my hack with get_query_var() ... ;)

vwasteels avatar Aug 20 '13 14:08 vwasteels

Stick with your code for now. I plan to add template tags in the future, and is_current_route() will be among them. But I can't give you a timeline. Pull requests are welcome. :)

jbrinley avatar Aug 20 '13 15:08 jbrinley

Ok good ! I'll try to do it, I let you know if I reach something ;)

vwasteels avatar Aug 20 '13 15:08 vwasteels