jbrinley / WP-Router

Routes paths to callback functions in WordPress
108 stars 26 forks source link

add is_route function #4

Open vwasteels opened 11 years ago

vwasteels commented 11 years ago

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

jbrinley commented 11 years ago
$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.

jonjhiggins commented 11 years ago

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:

vwasteels commented 11 years ago

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 );
jonjhiggins commented 11 years ago

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

vwasteels commented 11 years ago

you're welcome !

vwasteels commented 11 years ago

@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() ... ;)

jbrinley commented 11 years ago

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. :)

vwasteels commented 11 years ago

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