自定义插件-将数据从主页发布到插件中的另一个页面 [英] Custom Plugin - Posting data from main page to another page within your plugin

查看:94
本文介绍了自定义插件-将数据从主页发布到插件中的另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新的自定义插件,该插件添加了一个新的管理菜单以显示事件列表。当您单击任何给定事件时,我想将您发送到插件中的另一页,然后将呈现有关此事件的信息。我可以保持简单,只需使用查询字符串参数即可,因此无需执行POST表单,但我也会对此感兴趣。



有两页:


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

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

我的设置在基本页面(reservation-management.php)中:

  add_action('admin_menu','addReservationManagementMenuItem'); 

函数addReservationManagementMenuItem(){

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

}

在构建第一个屏幕的函数中,我渲染一些可点击的链接。为简化起见:

  function Reservation_management_building_function(){

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

< h2>可点击链接< / h2>
<?php echo(< a href =’reservation-management-details.php?id = $ id’> Event< / a>); ?>

<?php
}
}
?>

我只是简化了代码,删除了一些循环逻辑,等等,但是它可以工作并在循环所有事件,其URL为reservation-details.php?id = x,其中x是每个事件的唯一帖子ID。



事实是,这只是将我发送到未找到的页面。我什至尝试过使用诸如get_admin_url()等之类的东西。在wp-admin中。



如何使用href安全地将管理员用户发送到插件目录中的另一个管理员页面?



谢谢!

解决方案

已在插件中声明了页面?听起来您还需要在插件中添加一个子菜单页面。



示例(编辑):

  add_action('admin_menu','addReservationManagementMenuItem'); 

函数addReservationManagementMenuItem(){

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

//在此添加任何相对的子菜单页面
//您可以根据需要添加任意子菜单页面
//请记住,add_submenu_page()需要不同的设置
//传递给它的参数

//如果要查看子菜单页面,并且函数存在于
//插件定义页面中
add_submenu_page( ''reservation-management-details.php','Reservation Management详细信息','Reservation Management详细信息','manage_options','reservation_management_details_slug');

//如果不想在子菜单中访问子菜单页面
//使用null作为第一个参数,将其从子菜单中删除
//指定php文件使用
add_submenu_page(null,'Reservation Management Details','Reservation Management Details','manage_options','reservation-management-details.php');


}

在上面的示例中,能够发布到插件中的新页面,因为您已经定义了它。但是通常,我发现我希望任何人都只能进入我指定为 _POST的页面,因此我的第一个参数设置为 null



编辑:
我唯一不记得的就是POST网址必须是。



如果您有任何疑问,请告诉我。


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!

解决方案

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.

这篇关于自定义插件-将数据从主页发布到插件中的另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆