I know how to use hook_preprocess_page() to add/change variables for use in page.tpl.php. For nodes, you can use menu_get_object() to load the current node and process it from there.

How do I do this for a page I've created using hook_menu()? I've tried printing out all the $variables but there is nothing much I can identify this page with, apart from the 'theme_suggestions' which seems a bit flaky. Using arg(0) also seems like a hacky way to go.

Is there any way to set the page.tpl.php variable directly with hook_menu()? That would be ideal.

EDIT for clarification:

This is what I want to achieve:

function mymodule_menu() {

  $items['my-page'] = array(
    'title' => t('My Page'),
    'page callback' => 'mymodule_callback',
    'access callback' => TRUE,
    'page arguments' => array(),
    'page vars' => array('myvariable' => 'hello') // Obviously this doesn't work, but would be great


function mymodule_preprocess_page(&$vars) {

  if($something === 'my-page'){ // What to do here???
     $vars['myvariable'] = 'hello';


Basically I want something within hook_preprocess_page &$vars array to identify the route I have created with hook_menu. If I can define this somehow in hook_menu itself (or the callback) it would be even better.

I can use arg(0) but that doesn't account for any subpages, and I might want to change the route which means I need to change preprocess function too. Is there a better way?

  • 1
    hook_menu() simply creates a path on your system. It has no direct relationship to the template that outputs that path. Can you edit your question and give a specific example of what you want to happen, rather than talking in generalities? It would make it easier to understand your goal.
    – Jaypan
    Commented May 18, 2017 at 8:36
  • I agree with @Jaypan. The answer to your question as it is now is no, but that is not much helpful to you. If you edit your question to make it more specific to what you are trying to achieve, it will get a more helpful answer, for you and for future users.
    – apaderno
    Commented May 18, 2017 at 9:01

1 Answer 1


Just as an option, you could use a specific page template for your path, and sub-paths.

 * Implements hook_preprocess_page
function HOOK_preprocess_page(&$variables) {
  // Optional logic to check path condition
  // e.g arg(), request_uri(), current_path() etc..
  $some_condition = FALSE;
  if(arg(0) == 'base' || $some_condition) {
    // Set single page--base.tpl.php template, for /base and /base/subpath
    // Note: this overrides theme_hook_suggestions[]
    $variables['theme_hook_suggestion'] = 'page__base';
    // Sample template variable
    $variables['foo'] = 'bar';


Then, in your theme, you can copy page.tpl.php to page--base.tpl.php and add whatever page specific markup you need in the template.

You could also add logic and template variables to hook_preprocess_page. They would be available in your custom template. They can also be added there for the default page.tpl.php template. In any case, you will need some condition check for the specific target path match there.

Some relevant API functions to check the URL path include:

Note: arg() is also used by core template_preprocess_page to set the theme suggestions.

Uses the arg() function to generate a series of page template suggestions based on the current path.

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.