0

i have created new custom menu that act like link to some page using this code:

add_menu_page( __( 'Gallery', '' ), __( 'Gallery', '' ), 'edit_posts', 'post.php?post='. $gallery_page_id .'&action=edit', '', 'dashicons-admin-gallery', 6 );

the problem is that, when i click on this link, active class become on "pages" and not on my custom menu link (i know this is part of pages).

the question is how to add active class to my custom link and remove from the "pages".

0
+50

Well, it is quite a hack I propose, but I would suggest the following:

add_action( 'admin_head', 'chg_menu_on_special_pages_page' );
function chg_menu_on_special_pages_page(){
    global $parent_file;
    $gallery_page_id = 2;
    if( $parent_file != 'edit.php?post_type=page' )
        return;

    if( ! isset( $_GET['post'] ) || $_GET['post'] != $gallery_page_id )
        return;

    $parent_file = 'post.php?post='. $gallery_page_id .'&action=edit';
    return;
}

By the global $parent_file it is decided, which menu gets highlighted. We hook into the admin_head-action, the last action hook before the menu gets displayed. WordPress already decided, which is the $parent_file, in the case of your gallery it would be the "edit.php?post_type=page".

So, what do we do? We check if we are on our "Gallery"-Edit page and if so, we switch $parent_file to the URL, we've registered earlier using add_menu_page(). In my code, I was setting $gallery_page_id = 2; to test it.

Files in the admin, you might be interested in are wp-admin/menu-header.php. Variables, which you might be interested in are $menu, $submenu, $parent_file.

I hope, this helps you.

0

Perhaps you could define a function which just pulls in the post.php content and set it as your callback for add_menu_page(). But the slug parameter is not meant to accept variables. You will need to modify the $_REQUEST array to achieve that.

Ex:

add_menu_page( __( 'Gallery', '' ), __( 'Gallery', '' ), 'edit_posts', 'gallery-edit', 'gallery-edit-page', 'dashicons-admin-gallery', 6 );
function gallery-edit-page() {
  $gallery_page_id = 0; // change to the ID of your gallery page
  $_REQUEST['post'] = $gallery_page_id;
  $_REQUEST['action'] = 'edit';
  include('post.php');
}
  • I don't know if this will break the ability to save changes to your custom page. Try it and let me know. – dancriel Apr 8 '15 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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