A Very Quick Introduction to Drupal's hook_menu() and hook_theme()

Disclaimer: This post describes how to create custom pages on your drupal site, and create custom menu items (page routes), within a module. After reading this post, you will know how to implement hook_theme() and hook_menu() to create custom urls (Paths) and very basic content templates in both Drupal 6 and Drupal 7. This tutorial assumes you know how to create and upload files to your server using FTP, and also know how to create a basic custom module to use as a foundation. If you do not know how to create a basic custom module, check out our quick tutorial on the subject.

NOTE: For practice, we will be adding comments to almost all lines of code.

1. Creating a hook

Hooks in Drupal are just ways of modifying the website page's results.
Let's create one in our new module.

There are two hooks we will be using in this exercise: hook_menu and hook_theme. These links will take you to the drupal api pages that describe each hook.

New let's edit the custom_example.module, and make its contents the following:
<?php
/*
* Implementation of hook_menu()
*/
function custom_example_menu(){
}

Now here is the important part about using a hook:Copy the hook function from Drupal's API page, and replace the word hook with your module's name.
Your module is now "hook"ed into Drupal's menu system.

If you visit the hook_menu api page, you will see that there are many more options available, but this is all we will need for this introductory tutorial.

Next, we need to create the callback function, since we named one up above. Add the following below your hook_menu function:
<?php
/*
* Returns custom content to Drupal
*/
function my_page_function(){
}

We'll leave it empty for now and come back to it after the next section.

We've now hooked our module into the theme system. Let's look at what these items do:

'my_custom_template' => array(): This is the name of your template implementation.

'template' => 'custom-page': This key-value pair will create a call to a new template file that will look like this: 'custom-page.tpl.php'. Note: You don't have to add '.tpl.php' to the value. Also, this file is not created yet, we will do that below.

That's it for this part, now let's look at how to make our custom page return this new template file. Edit your module again, find your my_page_function function, and alter it to look like the following:
/*
* Returns custom content to Drupal
*/
function my_page_function(){
// Call theme() function, so that Drupal includes the custom-page.tpl.php template
return theme('my_custom_template');
}

What we're doing here is telling Drupal, "When a site visitor goes to /foo/bar, include my custom template".

Now the last thing you need to do is actually create that template. In the same directory as your module, create a new file titled 'custom-page.tpl.php'. Type the following (or anything you'd like) into it.
Hello World
That's pretty much it! Save the files, flush all your Drupal caches, and visit your new path. Here is what mine looks like: