Using a Block's visibility setting of Show block on specific pages: Pages on which this PHP code returns TRUE (experts only)...

enter image description here

...what PHP would I write that returns TRUE so that the block only shows on "Page Not Found" pages?

1 Answer 1


Can I recommend not doing it with PHP?

I would suggest making a page (via Admin->Content->Add Content), this assumes that you have the node module installed.

And then set that page as your "Default 404 (not found) page" in Admin->Configuration->System->Site Information

And then you can use that page's path in the block set up, but list under "Only the listed pages"

  • That was my first try! Seems simple, right? Doesn't work. When you hit an incorrect URL the Page Not Found page is shown, BUT it doesn't redirect. It retains the incorrect URL. So using its path in the block setup doesn't work… nor does using its node path ("/node/123").
    – Sam
    Jun 30, 2017 at 14:43
  • Instead of adding new node create a panel page. It works! I tried it a month ago!
    – Ismini
    Jun 30, 2017 at 14:47
  • Well, I am surprised by that, as drupal_deliver_html_page() api.drupal.org/api/drupal/includes%21common.inc/function/… is called and it sets/executes menu_set_active_item($path); and $return = menu_execute_active_handler($path, FALSE); using the 404 path you have set in config - and I thought blocks are also invoked from this. So with your 404 page set, your should be able to do compare your 404 path with menu_get_active_trail(); with php
    – rjl
    Jun 30, 2017 at 15:18
  • @rjl Well, you seemed pretty confident that this should work, so I double-checked the path option by adding both the aliased path and the node path to the Only listed pages box… and IT WORKED! I'm at a loss though; I literally spent an hour on this earlier trying every possible way to make this work using the path, and got nowhere. The only thing I did differently this time is use both path options at the same time… which I don't think has anything to do with it. Sigh… must be the Drupal gremlins hard at work again. Anyway, thanks for prompting me to try again!
    – Sam
    Jun 30, 2017 at 17:33
  • I am wondering if those gremlins may have been a caching issue, anyway I am glad it worked!
    – rjl
    Jun 30, 2017 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.