10

So i'm trying to create a WordPress plugin and I've created some menu pages using this provided function:

add_submenu_page('my_plugin_menu', 'Edit record page', 'Edit record page', 'manage_options', 'edit_record_page', array(&$this, 'display_edit_record_page');

and when I go to the page I notice on the address bar on the browser it reads something like this:

http://mydomain.com/wp/wp-admin/admin.php?page=edit_record_page

What I want to do is to be able to link this page but I find I have to hardcode the link for lack of a better way of doing it and I'm working on a dev site. So I was wondering how I could dynamically generate the link I saw on my browser so that when I copy this plugin code onto the production server it will work. Namely, is there a WordPress function that will generate the link portion of the submenu page create.

page=edit_record_page

Also, if I want to append query strings to the to the link is it as simple as adding it manually like so:

http://mydomain.com/wp/wp-admin/admin.php?page=edit_record_page&rec_id=1

or is there an appropriate WordPress function for doing that too?

16

admin_url() gets you the correct administration page URL (and network_admin_url() to get a network administration page URL)

Optionally, you can use add_query_arg() to append arguments to an URL, using an associative array:

$page       = 'edit_record_page';
$rec_id     = 1;
$record_url = add_query_arg(compact('page', 'rec_id'), admin_url('admin.php'));
3

<a href="admin.php?page=edit_record_page">Link Text</a>

and

<a href="admin.php?page=edit_record_page&rec_id=1">Link Text</a>

  • This works, however can you show an example of using admin_url(); inline like this? – Ben Racicot Jan 31 '14 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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