0

I'm trying to add top level menu items (not sub-menus) to my WP admin menu that link to edit a post. The reason for it, is that I have different sections and custom post types, so I'd like to have items in the menu that work like shortcuts.

So far I was able to create one using the code below, which isn't mine, but I adapted it. It works fine for just one item, but I'd like to create more (two or three) and I haven't been able to get it right.

function add_custom_menu_item(){
    add_menu_page( 'Slider Menu', 'Slider', 'manage_options', 'page_slug', 'function', 'dashicons-images-alt2', 6 );
} 
add_action( 'admin_menu', 'add_custom_menu_item' );

function custom_menu_item_redirect() {

    $menu_redirect = isset($_GET['page']) ? $_GET['page'] : false;

    if($menu_redirect == 'page_slug' ) {
       wp_safe_redirect( home_url('/wp-admin/post.php?post=1883&action=elementor') );
       exit();
    }
}
add_action( 'admin_init', 'custom_menu_item_redirect', 1 );

I'd appreciate any help.

Thanks.

3
  • Hello Carlos. You could just copy the add_menu_page() line for the other top-level links you want to create. See developer.wordpress.org/reference/functions/add_menu_page
    – Sally CJ
    Aug 23, 2018 at 10:39
  • Hello. Yes, thank you! I know that copying that add_menu_page() line allows me to create more top level items. What I don't know is how to add different redirects to each one of them.
    – Carlos
    Aug 23, 2018 at 10:57
  • I see. Please check my answer and let me know if that's what you're looking for?
    – Sally CJ
    Aug 23, 2018 at 13:39

1 Answer 1

1

how to add different redirects to each one of them

You can do an if-elseif-elseif... like this:

if ( condition 1 ) {
    // do something
} elseif ( condition 2 ) {
    // do something else
} elseif ( condition 3 ) {
    // do something else
}

So let's say you created three top-level admin menu items:

add_menu_page( 'Slider Menu', 'Slider', 'manage_options', 'page_slug', 'function', 'dashicons-images-alt2', 6 );
add_menu_page( 'Page Title', 'Menu Title', 'manage_options', 'page_slug2', 'function' );
add_menu_page( 'Page Title 2', 'Menu Title 2', 'manage_options', 'page_slug3', 'function' );

Then your if-elseif-elseif... would look something like this:

// For the "Slider" menu.
if ( $menu_redirect == 'page_slug' ) {
   wp_safe_redirect( admin_url( 'post.php?post=1883&action=elementor' ) );
   exit();
// For the "Menu Title" menu.
} elseif ( $menu_redirect == 'page_slug2' ) {
   wp_safe_redirect( 'URL here' );
   exit();
// For the "Menu Title 2" menu.
} elseif ( $menu_redirect == 'page_slug3' ) {
   wp_safe_redirect( 'URL here' );
   exit();
}

Btw, I think you should use admin_url() for getting the URL of an admin page. =)

5
  • 1
    That totally did it. Thanks Sally! I couldn't find much information on how to do this, so I hope your answer helps someone else too.
    – Carlos
    Aug 23, 2018 at 15:16
  • I'm glad it helped you! So, maybe you can accept that answer?.. ;)
    – Sally CJ
    Aug 23, 2018 at 16:23
  • Hi, I'd like to modify the above code so it works both with the English and Spanish versions of my site. Right now the code is taking into account the 'page_slug' string to redirect the user to the desired page. I wonder if there's a way to use the url as a string so depending on whether the user is on the /es or /en site, he's redirected appropriately.
    – Carlos
    Oct 8, 2018 at 14:39
  • On second though, I think a good approach could be to add the items for the Spanish version and then hide them based on whether the url is /es or /en. Any ideas on how to achieve that? Meaning I'd have two Slider items, but only one would be seen depending on language.
    – Carlos
    Oct 8, 2018 at 15:10
  • Hi @Carlos. I suggest you to post/ask a new question, and share its URL here. 🙂
    – Sally CJ
    Oct 15, 2018 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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