Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

  1. 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.
  2. If the above code is the best way to go, how do I add more items?
  3. How do I add the menu item below "Add new" (instead of below "tags")
share|improve this question
    
The approach you have is fine, the only thing i'd be concerned about is colliding indexes in the menu, you may not end up with the ordering you want without shuffling them around. –  t31os Apr 27 '13 at 19:44

1 Answer 1

up vote 1 down vote accepted

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$submenuas an associative array. Any items added to theedit.phparray 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 );
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.