Skip to content

Instantly share code, notes, and snippets.

@vanderb
Last active August 29, 2015 14:06
Show Gist options
  • Save vanderb/4828cabc887e32e40921 to your computer and use it in GitHub Desktop.
Save vanderb/4828cabc887e32e40921 to your computer and use it in GitHub Desktop.
Wordpress: Passing parameters through add_menu_page()
<?php
// Our parameters-array, where all params stored (for all custom pages)
$params = array();
add_action('admin_menu', 'my_admin_menu');
function my_admin_menu()
{
global $params;
$hook = add_menu_page('My Page', 'My Page', 'administrator', 'my-page', 'get_my_template');
// Set params for the above declared page-hook
$params[$hook] = array('template'=>'my-page.php', 'title' => 'My Page Title');
}
function get_my_template()
{
global $params;
// Get parameters for current page from param-array
$param = $params[current_filter()];
// Extract our params to variables (variable-names equal to array-keys from above)
// $template, $title, ...
extract($param);
// Sample include of template
include $template;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment