jbrinley / WP-Router

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

Make identify_route a public function #19

Closed chrisvanpatten closed 5 years ago

chrisvanpatten commented 9 years ago

Making identify_route a public function makes it possible to check which route you're currently on, making template re-use a bit easier in instances where there are only minor changes between routes.

For instance...

// my-signup-template.php

Signup form fields

<?php global $wp_query; if ( \WP_Router::get_instance()->identify_route( $wp_query ) === 'registerAdmin' ) : ?>
Special form fields for admins
<?php endif; ?>

Submit button

I can't think of any major security issues, but I might be missing something obvious!

chrisvanpatten commented 9 years ago

This would also theoretically fix #4. It's not a direct function, but with this it wouldn't be hard to make one...

/**
 * Check to see if the current route matches the desired route
 *
 * @param string $route_id
 * @param \WP_Query|null $query
 * @return bool
 */
function is_route( $route_id, $query = null ) {
    // Get the current query if the user doesn't provide one
    if ( is_null( $query ) ) {
        global $wp_query;
        $query = $wp_query;
    }

    // If the route ID matches the current route, return true
    if ( \WP_Router::get_instance()->identify_route( $query ) === $route_id )
        return true;

    // Otherwise, return false
    return false;
}