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'm working on a plugin that sets admin menu submenu pages on all different post types.

I need to be able to associate these new submenu pages with the corresponding post type of the top level menu item. Is this possible without having to rely on using $_GET?

I see that $typenow is used on edit.php, but I cannot use that on my custom page, as it only returns NULL.

So basically: How do I set appropriate/associate post type to a custom menu item.

UPDATED:

Here's an image that hopefully describes a little bit better what I'm after. http://i.imgur.com/4q4Dy.jpg

As it is now, the Custom Item in the submenu is only available when Pages is active since otherwise $_GET can't fetch post_type.

I'm using:

$post_type = esc_attr( $_GET['post_type'] );
$post_type_object = get_post_type_object( $post_type );    
$title = $post_type_object->labels->name;

$page = add_submenu_page( 'edit.php?post_type=' . $post_type, $title, $title, 'edit_pages', 'order-' . $post_type, 'cmspo_menu_order_page' );

So I need a way to grab the appropriate post type that the custom item should be associated with, in this case it's "page", but there will be custom items under each custom post type that's been registered with the capability_type "page" rather than "post".

share|improve this question
    
Something like this -> wordpress.stackexchange.com/questions/59417/… ? –  Joshua Abenazer Aug 7 '12 at 4:40
    
@JoshuaAbenazer No, I don't think so, but please elaborate if possible. I've updated my question to be (hopefully) more clear. –  INT Aug 7 '12 at 22:03
add comment

1 Answer

up vote 3 down vote accepted
+50

You need the information about the current post type in your callback function that renders the submenu page output. And at this point there is a lot of information ready:

add_action( 'admin_menu', 'wpse_60730_demo_submenu' );

/**
 * Register sub menu pages.
 *
 * Note that get_current_screen() is still NULL now.
 *
 * @wp-hook admin_menu
 * @return void
 */
function wpse_60730_demo_submenu()
{
    // get public post types
    $post_types = get_post_types( array ( 'public' => TRUE ) );

    foreach ( $post_types as $post_type )
    {
        add_submenu_page(
            "edit.php?post_type=$post_type",
            "Extra $post_type", // this should be made translatable
            "Extra $post_type", // this too
            "edit_{$post_type}s",
            "order-$post_type",
            'wpse_60730_demo_callback'
        );
    }
}
/**
 * Render the sub menu page output.
 *
 * All information is set now.
 *
 * @return void
 */
function wpse_60730_demo_callback()
{
    $screen = get_current_screen();
    global $typenow, $parent_file;
    print "<pre>get_current_screen()\n" . htmlspecialchars( print_r( $screen, TRUE ) ) . '</pre><hr>';
    print "<pre>\$typenow: " . htmlspecialchars( print_r( $typenow, TRUE ) ) . '</pre><hr>';
    print "<pre>\$parent_file: " . htmlspecialchars( print_r( $parent_file, TRUE ) ) . '</pre>';
}

Result

enter image description here

share|improve this answer
    
Solved the problem I was having, changed from edit_{$post_type}s to edit_pages and it all worked out. –  INT Aug 8 '12 at 14:46
add comment

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.