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 am trying to add a link, in the Admin Sidebar, that links to one of the pages I've created in the PAGES section. For example, I've created an ABOUT page. So in the sidebar I would like a link to "About" that loads this link: post.php?post=7&action=edit

Why? I am trying to do this because I am working on a website that has 4 custom posts types and 3 pages. The custom post types have admin menu links, so I think it would be a better user experience if the 3 pages also had direct links in the sidebar, instead of needing to click PAGES and then seeing the list of 3 pages and having to click "EDIT" (and then I would hide the PAGES section link from the admin menu).

I was headed down this road, but it doesn't seem to be what I want/need (I can't get it to link to where I want): http://codex.wordpress.org/Administration_Menus#Inserting_the_Pages

share|improve this question

2 Answers 2

up vote 0 down vote accepted

To dynamically add menu items you can use WP_Query, specifically get_posts or get_pages. Get pages is more consistent. Here's an example to add all pages to the Pages admin menu. You can change paramaters to exclude, include, orderby, etc in the $args below. To change to a custom post type just change $custom variable to your post type name.

/*-----------------------------------------------------------------------------------*/
/* All Pages Dropdown List */
/*-----------------------------------------------------------------------------------*/

if ( !function_exists( 'admin_menu_links_to_all_edit_post_type_custom' ) ) {
function admin_menu_links_to_all_edit_post_type_custom() {
    if ( !is_admin() ) // Only Run if On Admin Pages
        return;

     $custom = 'page';  // Change this to your custom post type slug ( So for "http://www.example.com/wp-admin/edit.php?post_type=recipes" you would change this to 'recipes'  )



      // Full List of Paramaters - http://codex.wordpress.org/Template_Tags/get_posts
      $args = array(
          'orderby'          => 'modified', //Orderr by date , title , modified, etc
          'order'            => 'DESC', // Show most recently edited on top
          'post_type'        => $custom, // Post Type Slug
          'numberposts'      => -1,  // Number of Posts to Show (Use -1 to Show All)
          'post_status'      => array('publish', 'pending', 'draft', 'future', 'private', 'inherit'),
      );
      $types = get_posts( $args ); // Get All Pages
      foreach ($types as $post_type) {
      add_submenu_page( // add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function );
          'edit.php?post_type='.$custom
        , esc_attr(ucwords($post_type->post_title)) // Get title, remove bad characters, and uppercase it
        , esc_attr(ucwords($post_type->post_title)) // Get title, remove bad characters, and uppercase it
        , 'edit_posts' // Require Edit Post/Page/Custom Capability
        , 'post.php?post=' . $post_type->ID . '&action=edit' // Get the page link by its id
        , '' // No function callback
      );    
      }
      wp_reset_postdata();
}
add_action('admin_menu', 'admin_menu_links_to_all_edit_post_type_custom');
}

if ( !function_exists( 'admin_menu_links_to_all_edit_post_type_custom_css' ) ) {
    function admin_menu_links_to_all_edit_post_type_custom_css() {
?>
<style type="text/css">
ul#adminmenu li.wp-has-submenu > ul.wp-submenu.wp-submenu-wrap {
max-height: 700px;
overflow-x: scroll;
}
</style>
<?php
   }
    add_action('admin_head', 'admin_menu_links_to_all_edit_post_type_custom_css');
}

Or to just add individually:

/*-----------------------------------------------------------------------------------*/
/* Single URL Submenu */
/*-----------------------------------------------------------------------------------*/

if ( !function_exists( 'single_submenu_dropdown_link_example' ) ) {
function single_submenu_dropdown_link_example() {
    global $submenu;
    $link_to_add = 'post.php?post=7&action=edit';
    // change edit.php to the top level menu you want to add it to 
    $submenu['edit.php'][] = array('About', 'edit_posts', $link_to_add);
}
add_action('admin_menu', 'single_submenu_dropdown_link_example');
}

This will add toplevel admin menus to your other pages:

/*-----------------------------------------------------------------------------------*/
/* Toplevel Page Menus */
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'toplevel_admin_menu_pages' ) ) {
function toplevel_admin_menu_pages(){
if ( !current_user_can('administrator') ) {  // If the user is not the administrator remove and add new menus
    remove_menu_page( 'index.php' );                  //Dashboard
    remove_menu_page( 'edit.php' );                   //Posts
    remove_menu_page( 'upload.php' );                 //Media
    remove_menu_page( 'edit.php?post_type=page' );    //Pages
    remove_menu_page( 'edit-comments.php' );          //Comments
    remove_menu_page( 'themes.php' );                 //Appearance
    remove_menu_page( 'plugins.php' );                //Plugins
    remove_menu_page( 'users.php' );                  //Users
    remove_menu_page( 'tools.php' );                  //Tools
    remove_menu_page( 'options-general.php' );        //Settings
    add_menu_page( 'Home', 'Home', 'edit_posts', 'post.php?post=39&action=edit', '', 'dashicons-admin-home', 6 );
    add_menu_page( 'About', 'About', 'edit_posts', 'post.php?post=15&action=edit', '', 'dashicons-editor-help', 7 );
    add_menu_page( 'Services', 'Services', 'edit_posts', 'post.php?post=24&action=edit', '', 'dashicons-admin-tools', 8 );
    }
  }
add_action( 'admin_menu', 'toplevel_admin_menu_pages' );
}
share|improve this answer
1  
Thanks Willow. I will try this now but I see this is going to add a submenu under an existing section. I am looking to add the pages links as top-level menu items - so instead of having Dashboard, Pages, Posts, Comments...I would like to have my pages listed About, Services, Contact, etc. Is this possible with an adjustment to your code? –  codeview Jul 28 at 16:03
    
Oh I got ya. I thought you wanted certain post types pages as sub menus to that post type. Yea thats even easier you just use add_menu_page. I changed the answer. –  willow Jul 28 at 21:47
    
Also current_user_can checks which user is active so this will only change for people not administrator. If you want to change for yourself to just delete that part. –  willow Jul 28 at 21:52
    
Ah! Thank you, Willow. That is exactly what I need. I was actually already using the add_action('admin_menu') function to remove_menu_page's. I'm having trouble reordering the menu now, though. I am using the menu_order function already but I don't know what to put in for a new line, for example, to have the new ABOUT link at the top? –  codeview Jul 28 at 22:31
    
function wpse_menu_order($menu_ord) { if (!$menu_ord) return true; return array( 'index.php', // Dashboard 'separator1', // First separator '_____________', // About? 'edit.php?post_type=event', // Custom Post Type: event ); } add_filter('custom_menu_order', 'wpse_menu_order'); // Activate wpse_menu_order add_filter('menu_order', 'wpse_menu_order'); –  codeview Jul 28 at 22:31
if ( !function_exists( 'wp_toolbar_frontend_admin_menu_links_extras' ) ) {

function wp_toolbar_frontend_admin_menu_links_extras($wp_admin_bar) {   
    if ( is_admin() || !is_admin_bar_showing() )
          return;

   if ( !current_user_can('edit_pages') ) 
        return;

    $wp_admin_bar->add_node(array(
        'id' => 'homepage',
        'title' => 'Home',
        'href' => admin_url() . 'post.php?post=39&action=edit' ,
        'parent' => 'site-name',
        'meta' => array(
            'class' => 'homepage'
        )
    ));

    $wp_admin_bar->add_node(array(
        'id' => 'aboutus',
        'title' => 'About',
        'href' => admin_url() . 'post.php?post=36&action=edit' ,
        'parent' => 'site-name',
        'meta' => array(
            'class' => 'aboutus'
        )
    ));

    $wp_admin_bar->add_node(array(
        'id' => 'services',
        'title' => 'Services',
        'href' => admin_url(). 'post.php?post=32&action=edit' ,
        'parent' => 'site-name',
        'meta' => array(
            'class' => 'services'
        )
    ));
}

    add_action('admin_bar_menu', 'wp_toolbar_frontend_admin_menu_links_extras', 99);

}
share|improve this answer
    
You can also try this one which will add those links to the front end toolbar as a submenu to the Dashboard link. Very convenient. –  willow Jul 28 at 22:15
    
Just saw your comment that this is for frontend admin toolbar. Thank you. I actually removed it with this: show_admin_bar(false); for this particular project (trying to really streamline Wordpress for this particular project) but it may come in handy for others. –  codeview Jul 28 at 22:42
    
Hey no prob... By the way, I personally try to do this kind of stuff without a plugin, but wordpress.org/plugins/admin-menu-editor is preferable than coding for a lot of people on this –  willow Jul 29 at 22:08

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.