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.

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?

share|improve this question
    
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 at 12:11
    
@engelen I mean the link rendered to the href attribute of the anchor tag. –  Pedr Aug 4 at 12:43

2 Answers 2

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 );
share|improve this answer
    
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. –  Pedr Aug 4 at 12:46

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! ;-)

share|improve this answer

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.