Skip to content

Instantly share code, notes, and snippets.

@rowatt
Last active January 4, 2016 22:19
Show Gist options
  • Save rowatt/8687230 to your computer and use it in GitHub Desktop.
Save rowatt/8687230 to your computer and use it in GitHub Desktop.
Customise WordPress admin menus
<?php
class Admin_Menus extends Presi_Core_Class
/**
* Keep track of which admin menu separator we are adding
* @var int
*/
private $separator_index = 0;
public function __construct()
{
//custom admin menu order
$this->add_action('admin_menu', 'custom_admin_menus', 99);
}
public function custom_admin_menus()
{
//remove link manager menu
remove_menu_page('link-manager.php');
//remove theme customisation submenu
if( !current_user_can('switch_themes') )
remove_submenu_page('themes.php','themes.php');
//add extra separators if required
//need to add in pairs if they are next to each other as WP dedups
//position as required in custom_menu_order method
$this->add_admin_menu_separator(900);
$this->add_admin_menu_separator(901);
//custom menu order
add_filter( 'custom_menu_order', '__return_true' );
$this->add_filter('menu_order', 'custom_menu_order' );
}
/**
* Add a menu separator
* @see http://wordpress.stackexchange.com/questions/2666/add-a-separator-to-the-admin-menu
*
* @param $position
*/
public function add_admin_menu_separator($position)
{
global $menu;
if( ! $this->separator_index )
{
foreach($menu as $section)
{
if (substr($section[2],0,9)=='separator')
$this->separator_index++;
}
}
$this->separator_index++;
$menu[$position] = array('','read',"separator{$this->separator_index}",'','wp-menu-separator');
ksort( $menu );
}
/**
* Custom admin menu order
*
* @param $menu_ord
*
* @return array|bool
*/
public function custom_menu_order($menu_ord)
{
$new_order = array(
'index.php', // Dashboard
'presi_tools', // Dashboard
'jetpack',
'wp-help-documents',
'separator1', // -------------------------
'edit.php?post_type=page', // Pages
'edit.php?post_type=hidden_page', //Hidden pages custom post type
'upload.php', // Media
'edit.php', // Posts
'edit-comments.php', // Comments
'gf_edit_forms', //Gravity Forms
'edit.php?post_type=testimonials-widget',
'separator2', // -------------------------
'edit.php?post_type=custom_post_type',
'separator', // -------------------------
'themes.php', // Appearance
'genesis',
'plugins.php', // Plugins
'users.php', // Users
'tools.php', // Tools
'options-general.php', // Settings
'separator-last', // -------------------------
);
$leftovers = array_diff( $menu_ord, $new_order );
return array_merge( $new_order, $leftovers );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment