1

I am trying to add a Wordpress admin menu item that will point to a URL which is

http://docs.[currentdomain]

My plugin code is currently:

function user_manual() {
    add_menu_page(
        __( 'Custom Menu Title', 'textdomain' ),
        'Manual',
        'manage_options',
        'http://docs' . home_url(),
        '',
        'dashicons-book-alt',
        1
    );
}
add_action( 'admin_menu', 'user_manual' );

But what I end up with is currently: http://docshttp/www.domain.com

How can I achieve this without hardcoding the domain name in?

1 Answer 1

1

The way of your approach is arguably wrong. One shouldn't use add_menu_page for external links as that function isn't created for that purpose. Add menu page adds a page in the admin area with menu_slug as the fourth argument.

If we take a look at add_menu_page/dev ref source, we can notice that whatever the URL we pass in the function is passed through plugin_basename / dev ref which normalizes and replaces the path giving us the path relative to plugin directory.


Instead you can hook it to admin bar which in my opinio correct place to put external link(if this is only purpose)

function wpse243214_user_manual($wp_admin_bar) {
    $wp_admin_bar->add_menu( array(
        //'parent'    => 'wpse-parent-id', // parent if any
        'id'        => 'wpse243214_usermanual',
        'title'     => __('User Manual'),
        'href'      => 'http://docs.'.basename(esc_url(home_url())),
    ) );
}
//change priority(15) accordingly
add_action( 'admin_bar_menu', 'wpse243214_user_manual', 15 );

With this we get

WP Admin Bar

Also note that I used basename for home_url otherwise we get a link with http or https scheme (http://www.example.com).

1
  • That works nicely however there's two issues: 1. If the visitor happens to be visiting "WWW.domain.com", the link ends up as "docs.www.domain.com" 2. I really did prefer it in the sidebar, below "Dashboard" with the icon because I need to be sure it's as prominent and high visibility as possible.
    – Jay
    Commented Oct 19, 2016 at 17:02

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.