Using MENU_LOCAL_TASK tabs to link around the site

I'm sure many of you have been asked this by your clients:
"Can we add a link up the top there to create a new (listing/page/article)?"

This isn't difficult, I'm merely sharing with you a way to make this sort of UI tweak much simpler.

Normally, I would have created a new menu item in hook_menu, and in the custom page callback do a drupal_goto.


<?php
/**
* Implement hook_menu
*/
function mymodule_menu() {
$menu['user/%user/add_listing'] = array(
'title' => 'Add a Listing',
'description' => 'Click here to create a new listing',
'type' => MENU_LOCAL_TASK,
'page callback' => 'mymodule_redirect_add_listing',
'access callback' => 'user_is_logged_in',
);
return $menu;
}

/**
* Page callback which redirects to the add listing page
*/
function mymodule_redirect_add_listing() {
// this takes them to the listing add page and then redirects back to the user page they came from
drupal_goto('node/add/listing', 'destination=user');
}
?>

While the above example gets the job done, it can be done in a single step rather than 2:

<?php
/**
* Implement hook_menu
*/
function mymodule_menu() {
$menu['user/%user/add_listing'] = array(
'title' => 'Add a Listing',
'description' => 'Click here to create a new listing',
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_goto',
'page arguments' => array('node/add/listing', 'destination=user'),
'access callback' => 'user_is_logged_in',
);
return $menu;
}
?>

By changing the page callback to drupal_goto and adding the destination as the page argument, there is no need for a page callback which does exactly the same thing.

So for all you neat freaks out there, this can save you up to 8 lines of custom code per redirect!!