Custom Plugin - Posting data from main page to ano

2019-08-16 07:45发布

问题:

I created a new custom plugin, that adds a new admin menu to show a list of events. When you click any given event, I want to send you to another page in my plugin that will then render information about this event. I can keep it simple and just use a query string parameter, so don't need to do a form POST, but I would be interested in that as well.

There are two pages:

  1. /my-plugin/reservation-management.php
  2. /my-plugin/reservation-management-details.php

My setup in the base page (reservation-management.php):

add_action( 'admin_menu', 'addReservationManagementMenuItem' );

function addReservationManagementMenuItem(){

add_menu_page('Reservation Management', 'Reservation Management', 'manage_options', 'reservation_management_slug', 'reservation_management_building_function','',3);

} 

Inside my function to build out the first screen, I render some clickable links. To simplify:

function reservation_management_building_function(){

if(!current_user_can('manage_options')){
  ?>

    <h2>Clickable Link</h2>
    <?php echo("<a href='reservation-management-details.php?id=$id'>Event</a>"); ?>

 <?php 
   }
}
?>

I just simplified the code, removed some loop logic, etc, but it works and renders out in a loop all of the events with a url of reservation-details.php?id=x where x is the unique post id of each event.

The thing is, this just sends me to a page not found. I even tried using things like get_admin_url() etc

I think I'm missing a fundamental step in how a custom plugin can post from one page to another all while still being within wp-admin.

How can I use an href to safely send the admin user to another admin page within my plugin directory?

Thanks!

回答1:

Has the page been declared in the plugin? It sounds like you'd need to add a submenu page to your plugin as well.

Example (edited):

add_action( 'admin_menu', 'addReservationManagementMenuItem' );

function addReservationManagementMenuItem(){

add_menu_page('Reservation Management', 'Reservation Management', 'manage_options', 'reservation_management_slug', 'reservation_management_building_function','',3);

//add any relative submenu pages here
//you can have as many submenu pages as you need
//please keep in mind that add_submenu_page() requires a different set 
//of parameters passed to it

//if you want the submenu page viewed, and your function exists on your 
//plugin definition page
add_submenu_page('reservation-management-details.php', 'Reservation Management Details', 'Reservation Management Details', 'manage_options', 'reservation_management_details_slug'); 

//if you don't want your submenu page accessible in the submenu
//use null as the first parameter to remove it's inclusion from the submenu
//specify the php file used 
add_submenu_page(null, 'Reservation Management Details', 'Reservation Management Details', 'manage_options', 'reservation-management-details.php');


} 

In the above example, you'd be able to post to your new page in your plugin, because you've now defined it. But often times, I find that I don't want anyone being able to get to a page I've designated as '_POST' only, so my first parameter is then set to null.

Edit: The only thing that I can't remember off the top of my head is what that POST url needs to be. Please let me know if you need assistance with that, and I'll work it out for you.

Please let me know if you have any questions.