7

Trying to add a link to my admin menu (dashboard/backend) that leads to the frontend of wordpress, so I basically want a link in the menu that takes me to the blog.

I've found a couple of ways to add new stuff to the admin menu, but I haven't been able to link to the home_url, it seems like the admin menu only is designed to let you link to stuff within the wp-admin. Anyone that have succeeded with this?

Edit:
Tried with this: https://gist.github.com/792b7aa5b695d1092520

add_admin_menu_item('Overview',array( 
      'title' => 'View Site',
      'slug' => get_bloginfo('url')
  ));

Which gives me the link hxxp://myurl.com/myurl.com
With this plugin (hxxp://wordpress.org/extend/plugins/admin-menu-editor/), I get:
hxxp://myurl.com/wp-admin/www.test.com

2

5 Answers 5

7

you can create a function that redirects to the front-end like this:

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

and call that function in WordPress default add_menu_page function like this:

add_menu_page( 'redirecting', 'View Site', 'read', 'my-top-level-handle', 'redirect_home_987');

Hope this helps

3
  • Not sure if I understand you correctly. I've added this to my functions.php now: add_action('admin_menu', 'add_viewsite'); function add_viewsite() { add_menu_page( 'redirecting', 'View Site', 'read', 'my-top-level-handle', 'redirect_home_987'); } // Call this function for a "home" link function redirect_home_987(){ wp_redirect( home_url() ); exit; } Which gives me a link that looks like this: url.com/wp-admin/admin.php?page=my-top-level-handle
    – INT
    Commented Jan 31, 2011 at 14:15
  • 2
    I had the same problem - some of the headers had already been sent. Tried a few options; the only one that seemed to work (which I'm not really happy with) is combining the above with javascript, like this answer
    – Hobo
    Commented Jul 7, 2013 at 18:59
  • 1
    It will throw a "headers already sent" error.
    – Joel James
    Commented Aug 22, 2019 at 9:10
1

The name of site (top left, next to WP logo) links to front-end, is that not sufficient?

Also it would help if you included code that you had tried so far in the question.

1
  • Thanks for your reply. I removed the name of the site, since it broke the layout of my customized backend, so I need to implent a link to the front end in the menu instead.
    – INT
    Commented Jan 30, 2011 at 23:16
1

You cannot add items to the admin menus that point to external URLs, the menu addition functions do not support this, nor will they allow it.

However, if you attach a callback onto the admin_menu hook, you can insert some items yourself manually and totally overcome the non-external URL problem. I do this myself and i'm quite surprised noone has already pointed out the issue with external menu links.

I provided an example when answering Adding an Arbitrary Link to the Admin Menu?

Let me know if that helps.

2
  • In fact, this question seems to be an exact duplicate of this one. So it got my vote to close it!
    – Jan Fabry
    Commented Feb 1, 2011 at 11:12
  • @Jan Fabry - I was thinking that when i linked to the other question so i've also voted to close as duplicate (the user should also have the answer he/she is looking for now, so closing need not be considered a bad thing).
    – t31os
    Commented Feb 1, 2011 at 11:16
1
add_action('admin_menu', 'example_admin_menu');

/**
* add external link to Tools area
*/
function example_admin_menu() {
    global $submenu;
    $url = 'http://www.example.com/';
    $submenu['tools.php'][] = array('Example', 'manage_options', $url);
}
-1

just add ../ as slug prefix:

add_admin_menu_item('Overview',array( 
  'title' => 'View Site',
  'slug' => '../slug'          // relative url
));
1
  • 1
    Please edit your answer, and add an explanation: why could that solve the problem?
    – fuxia
    Commented Oct 20, 2017 at 12:30

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.