I made it possible to use post-new.php?post_cat=catslug in the wordpress admin for creating a new post with a pre-filled category by using the answer here:
How to add category to: ‘wp-admin/post-new.php’?
Now I want to create multiple menu items with this format under the “Posts” -> “Add new” admin menu item. So the menu will be:
POSTS
- All posts
- Add New
- Add New Profile -> post-new.php?post_cat=profile
- Add New News -> post-new.php?post_cat=news
- Add New Pitch ->post-new.php?post_cat=pitch
- Categories
- etc.
I have been able to add one item to the bottom of the menu with this code in functions.php
add_action( 'admin_menu' , 'admin_menu_new_items' );
function admin_menu_new_items() {
global $submenu;
$submenu['edit.php'][500] = array( 'Add new profile', 'manage_options' , '/wp-admin/post-new.php?post_cat=pitches' );
}
Questions:
- Is this the best way to do this? Or do I need to use one of the functions as described here http://codex.wordpress.org/Administration_Menus. I tried some but I couldn’t get it to work the way I want.
- If the above code is the best way to go, how do I add more items?
- How do I add the menu item below “Add new” (instead of below “tags”)
To see the the current array keys try this:
add_action( 'admin_menu' , 'admin_menu_new_items', 1 );
function admin_menu_new_items() {
global $submenu;
wp_die( '<pre>' . var_export( $submenu['edit.php'], true ) . '</pre>' );
}
I get this:
array (
5 =>
array (
0 => 'All Posts',
1 => 'edit_posts',
2 => 'edit.php',
),
10 =>
array (
0 => 'Add New',
1 => 'edit_posts',
2 => 'post-new.php',
),
15 =>
array (
0 => 'Categories',
1 => 'manage_categories',
2 => 'edit-tags.php?taxonomy=category',
),
16 =>
array (
0 => 'Tags',
1 => 'manage_categories',
2 => 'edit-tags.php?taxonomy=post_tag',
),
)
Index ’10’ is the Add New item. To add the new sub menu items use indexes 11, 12 and 13:
$submenu['edit.php'][11] = array( 'Add New Profile', 'manage_options' , '/wp-admin/post-new.php?post_cat=pitches' );
$submenu['edit.php'][12] = array( 'Add New News', 'manage_options' , '/wp-admin/post-new.php?post_cat=news' );
$submenu['edit.php'][13] = array( 'Add New Pitch', 'manage_options' , '/wp-admin/post-new.php?post_cat=pitch' );
When WordPress adds the sub menu items to the dashboard it treats$submenu
as an associative array. Any items added to theedit.php
array will be added to the end of array regardless of the key used. Use ksort() to order the keys.
ksort( $submenu['edit.php'], SORT_NUMERIC );
Putting it all together:
add_action( 'admin_menu' , 'admin_menu_new_items', 1 );
function admin_menu_new_items() {
global $submenu;
$submenu['edit.php'][11] = array( 'Add New Profile', 'manage_options' , '/wp-admin/post-new.php?post_cat=pitches' );
$submenu['edit.php'][12] = array( 'Add New News', 'manage_options' , '/wp-admin/post-new.php?post_cat=news' );
$submenu['edit.php'][13] = array( 'Add New Pitch', 'manage_options' , '/wp-admin/post-new.php?post_cat=pitch' );
// WordPress treats $submenu as an associative array and does not sort it first.
// We have to sort keys into the order we want them to show up.
ksort( $submenu['edit.php'], SORT_NUMERIC );
}