0

The closest I've managed to get is:

function redirect_to_local_110(){
  wp_redirect("/", 302);
  exit;
}

function add_home_link() {
    add_menu_page( 'Course', 'Course', 'read', 'home', 'redirect_to_local_110', 'dashicons-welcome-learn-more');
}

add_action( 'admin_menu', 'add_home_link', 1001 );

However, even though the function redirect_to_local_110 is called, the rendered link is admin.php?page=home.

How can I have a top-level menu item that links to the homepage?

2
  • What exactly do you mean by "the rendered link is admin.php?page=home? Does the menu item redirect to the home page?
    – engelen
    Aug 4, 2014 at 12:11
  • @engelen I mean the link rendered to the href attribute of the anchor tag. Aug 4, 2014 at 12:43

2 Answers 2

0

The $location parameter was incorrect. Try this:

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

function add_home_link() {
    add_menu_page( 'Course', 'Course', 'read', 'home', 'redirect_to_local_110', 'dashicons-welcome-learn-more');
}

add_action( 'admin_menu', 'add_home_link', 1001 );

Or you could use this:

function redirect_to_local_110(){
 wp_redirect( 'http://www.example.com', 301 );
  exit;
}

function add_home_link() {
    add_menu_page( 'Course', 'Course', 'read', 'home', 'redirect_to_local_110', 'dashicons-welcome-learn-more');
}

add_action( 'admin_menu', 'add_home_link', 1001 );
1
  • It makes no difference if I hard-code a url or grab it using home_url() in redirect_to_local_110. The url in the menu item anchor tag will be admin.php?page=home either way. Aug 4, 2014 at 12:46
0

The HTTP headers are already sent when you are inside your redirect_to_local_110() function, so you risk the :

Warning: Cannot modify header information - headers already sent by ...

message.

You can try and verify it within your function:

function redirect_to_local_110()
{
    if( headers_sent() )
    {
        echo 'oh, headers are already sent, so we better not use wp_redirect() here!';
    }
}

Another (hacky) option would be to modify the link with Javascript, something like:

add_action( 'admin_footer', function(){
    $link = home_url( '/' ); // Modify to your needs.
    echo '<script>
        jQuery( function($){
            $("#adminmenu a.toplevel_page_home").attr( "href", "' . esc_url( $link ) . '" );
        });
    </script>';
});

ps: it would be probably be more clean to use wp_localize_script() and wp_enqueue_script(), but it feels a bit too much to load an external javascript file with only one short line! ;-)

Your Answer

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

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