views:

28

answers:

1

Hi,

I'm building a Wordpress plugin and I added a menu page which serves for management of "Vendor" entities that are kinda similar to the existing User entities.

I have a list page similar to Users List, with Edit button for every record and when I click on the "Edit" button I should be taken to the "Edit Vendor" (which does not have its submenu item in the admin menu) page for that vendor. Everything is stored in the "plugins/wp_vendors" folder.

Question: What URL should I use for opening that Edit page? How should a slug be registered for the Edit Vendor page?

PS. Vendor List is added to the admin menu with

add_menu_page('Vendors', 'Vendors', 8, 'C:\wordpress\wp-content\plugins\wp-vendors\vendors-list.php');

And I can open the List page with

http://localhost/wp-admin/admin.php?page=wp-vendors/vendors-list.php

Can anyone help me on this?

+1  A: 

Well, first I'd suggest modifying the initial add_menu_page call:

add_menu_page( 'Vendors', 'Vendors', 'manage_options', 'wp-vendors', 'my_admin_page_callback' );

The user level argument is deprecated, so it's better to use capabilities. Also, it's better to use a callback function for the admin page, since having plugin files output data by default can lead to unexpected errors.

Now, there are a few ways you can do what you're asking. You can either register a new submenu:

add_submenu_page( 'wp-vendors', 'Edit Vendor', 'Edit Vendor', 'manage_options', 'wp-vendors-edit', 'my_admin_edit_page_callback' );

And have that function check for a vendor id to edit. If it doesn't exist, redirect them back to the main vendors menu page.

With this method, the url would look like this:

http://localhost/wp-admin.php?page=wp-vendors-edit&vendor=<vendor ID>

The other way is just to use URL arguments and separate your admin page callback with if checks. For example:

if( ( isset($_GET['action']) && $_GET['action'] == 'edit' ) && ( isset($_GET['vendor']) && !empty($_GET['vendor']) ) ){
  //You're editing a vendor.
} else {
  //You're listing the vendors.
}

Then make the link for editing look like this:

http://localhost/wp-admin.php?page=wp-vendors&action=edit&vendor=<vendor ID>
John P Bloch