Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Admin URL to my plugin's page

Tags:

wordpress

My plugin is basically a link display page, for instance if you want to display a page with links to other websites.

In wp-admin I have a menu item on the left side bar added with this code:

function bls_add_menu_page() {
    add_menu_page('Custom Links', 'Custom Links', 'manage_options', 
                  'customlinks', 'bsl_admin_page', '', 15);
}

After adding a new link, I want to redirect to my plugin home page in admin. The URL when I click on my plugin menu link is :

localhost/wp-admin/admin.php?page=customlinks

How do I get that URL in Worpdress? Currently I just do this :

wp_redirect('/wp-admin/admin.php?page=customlinks'); 

but I hope there is a better way of getting my plugin admin URL?

like image 990
Joshua Avatar asked May 23 '13 11:05

Joshua


2 Answers

You get the concrete URL to admin.php by using the admin_url function:

admin_url('admin.php'); # http(s)://localhost/wp-admin/admin.php

That function chooses the proper sheme (http/https) based on your Wordpress configuration for you so you do not need to care about it. Same for the path to the admin. The only thing you need to specify is the file name (admin.php).

And in your concrete example you add the page query-info part:

$url = admin_url('admin.php?page=customlinks');
wp_redirect($url); 
like image 125
hakre Avatar answered Oct 21 '22 08:10

hakre


URL for menu page or options page has 'page' parameter ( page slug defined in add_menu_page() or add_options_page() ). You can always get the current page from $_GET['page'] param, so URL for the options page is:

admin_url( "options-general.php?page=".$_GET["page"] )

, and URL for menu page ( actually it works with options pages also ) is:

admin_url( "admin.php?page=".$_GET["page"] )
like image 22
Danijel Avatar answered Oct 21 '22 08:10

Danijel