Redirect from add_menu_page

I have added a menu using add_menu_page which all works correctly, but when clicked I want this menu page to open the post editor for a particular post id.

As a proof of concept i have tried echoing a javascript redirect out into the do function like so…

Read More
// Load up the menu page
 function register_availability_custom_menu_page() {
   add_menu_page('custom menu title', 'Availability', 'add_users', 'options',             'options_do_page');
}

function options_do_page() {
echo "<script type="text/javascript">";
echo "window.location = '/whatever_page.php';";
echo "</script>";
}

This approach does work but I was wondering if it is the best approach, is there a better way to redirect to the page I am after?

UPDATE

I have now also tried using wp_redirect with this code…

add_action( 'admin_menu' , 'admin_menu_new_items' );

function admin_menu_new_items() {
    wp_redirect( home_url() );
    exit;
}

This gives me a Headers already sent error, can anyone suggest where I am going wrong?

Related posts

Leave a Reply

3 comments

  1. If I’m understanding this correctly, you don’t need the redirect. Instead of using a $menu_slug in the function add_menu_page, put the address of the target page, e.g.:

    $the_post_title = 'The Portfolio';
    
    add_action( 'admin_menu', 'wpse_59050_add_menu' );
    
    function wpse_59050_add_menu() 
    {
        global $the_post_title;
        $our_page = get_page_by_title( $the_post_title );
    
        $settings_page = add_menu_page( 
            'Edit '.$our_page->post_title, 
            'Edit '.$our_page->post_title, 
            'add_users', 
            '/post.php?post='.$our_page->ID.'&action=edit', 
            '', 
            '', 
            2
        );
    }
    

    This function is from the following WordPress Answer: Highlighting a Menu Item by Post Name. You’ll need some jQuery to do the correct highlighting of this top level menu, check both my and TheDeadMedic answers in that Q.

    This other one is useful too: Add highlighting to new Admin Dashboard Menu Item.

  2. Recently did this for pootle page builder, and this AFAIK is the best way,

    /**
     * Redirecting from Page Builder > Add New page
     * @since 0.1.0
     */
    function add_new() {
        global $pagenow;
    
        if ( 'admin.php' == $pagenow && 'page_builder_add' == filter_input( INPUT_GET, 'page' ) ) {
            header( 'Location: ' . admin_url( 'whatever-page.php' ) );
            die();
        }
    }
    add_action( 'admin_init', 'add_new' );
    

    Replace page_builder_add with your page slug and admin_url( 'whatever-page.php' ) with url you wanna redirect to.

    We always use scrutinizer-ci for best code practices and maintain code score greater than 9.5/10, so all code (Including this) is secure and optimized 😉

    Lemme know how it works for ya.

    Cheers

  3. I found a 2-step solution that doesn’t use JS/JQ.

    Step 1:
    Near the top of your script, put the following PHP to display CSS that hides your first page:

    add_action('admin_head', 'hide_my_first_page');
    
    function hide_my_first_page(){
        echo '<style>
            a[href="admin.php?page=admin_menu_slug"] + ul > li.wp-first-item{
                display: none;
            }
        </style>';
    }
    

    Where admin_menu_slug is the 4th argument passed to add_menu_page();.

    Step 2:
    Take the function of the page you want to run and pass it as the 5th argument in add_menu_page();