I need to test a series of links to see if any of them match the page that is being processed. I'm having trouble finding a solution that will match all possible pages (a node, a view page, a page created by a module, etc) and will consider path aliases.

(It would be convenient to start with all parameters of $test_url stripped of the domain name and leading/following slashes.)

function _is_current_page($test_url){
   $test = FALSE;
   return $test;

Test case 1:

// current Url: domain.com/blog/year/month/article_name
// current Nid: 1234
// alias for above: domain.com/popular_article_name

_is_current_page("blog/year/month/article_name") -> true
_is_current_page("node/1234") -> true
_is_current_page("popular_article_name") -> true

Test case 2:

// current Url: domain.com/blog/calendar
// current Nid: _none_ (generated by view or module)
// alias for above: domain.com/blog-calendar

_is_current_page("blog/calendar") -> true
_is_current_page("blog-calendar") -> true

Help greatly appreciated : )


Excluding edge cases (querystring, hash tags, etc), this seems to work for all my test cases:

 * Determine whether a given url matches the current page.
 * @param string $test_path     A file path (no domain, no querystring, etc)
 * @return boolean 
function _nwr1_is_current_url($test_path){
    $test_path = trim($test_path, '/');
    $test_norm_path = drupal_lookup_path('source', $test_path);
    $this_nid = (arg(0) == 'node' && is_numeric(arg(1))) ? arg(1) : FALSE;
    $this_norm_path = ($this_nid) ? "node/$this_nid" : FALSE;
    $success = FALSE;   

    // test by source (eg node/XXXX - preferred)
    if($test_norm_path && $this_norm_path && ($test_norm_path==$this_norm_path) ){
        $success = TRUE;

    // test by path 
    }else if($test_path && current_path() && ($test_path==current_path()) ){
        $success = TRUE;
    return $success;

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.