[英]How can I set a php header redirect in a wordpress admin page
編輯:我已經擴展了示例代碼以包括一個基本的用例。
我正在嘗試為wordpress中的插件創建管理頁面。 我已成功使用法典中的說明制作了一個簡單的管理頁面。 但是,我目前陷入困境。 我正在嘗試使用
header("Location: /myLocation");
調用,但我無法設置標題,因為在加載管理頁面時,顯然已經輸出了其他HTML。 我想使用標題重定向,以便為管理頁面構建的表單處理程序可以使用POST到GET模式。 (使用post將表單發送回自身,處理post數據,使用get將信頭重定向回自身,這樣可以防止重新發送和表單重新提交警告)
有什么方法可以修改設置,以便能夠調用header()
?
我在這里遵循了該教程, 網址為http://codex.wordpress.org/Adding_Administration_Menus
這是我的代碼,當前位於mu-plugin
。 使其成為常規插件會影響我使用header()?
能力header()?
如果可能的話,我寧願使用mu-plugin
,但如果需要的話,我會使用常規mu-plugin
。
這是代碼,除了對header()
的調用外,其他所有東西都工作正常。 它只是本教程中給出的示例,並帶有一些我自己的文字。
謝謝閱讀!
<?php
/** Step 2 (from text above). */
add_action('admin_menu', 'my_plugin_menu');
/** Step 1. */
function my_plugin_menu() {
add_menu_page('My Plugin Options', 'Manage Articles', 'manage_options', 'manageArticles', 'manage_articles');
}
/** Step 3. */
function manage_articles() {
if (!current_user_can('manage_options')) {
wp_die(__('You do not have sufficient permissions to access this page.'));
}
?>
<?php
//this block of code is an example of what I am trying to do
if (isset($_POST['formSubmit']) && $_POST['formSubmit']==='true')
{
global $wpdb;
$wpdb->query("UPDATE myTable set name='{$_POST['something']}' where id='{$_POST['id']}'");
header("Location: manageArticles?updated");
}
if (isset($_GET['updated']))
{
echo "you have updated the table!";
}
?>
My admin page
<form name="theForm" action="" method="post">
Something: <input type="text" value="something" name="something"><br>
Row ID: <input type="text" value="1" name="id"><br>
<input type="hidden" value="true" name="formSubmit">
<input type="submit">
</form>
<?php
}
?>
所以...我還有更多關於鈎子的知識。 由於這是一個mu-plugin
,根據此處的管理掛鈎列表,我可以使用muplugins_loaded掛鈎
因此,將其放在插件頂部
<?php
add_action('muplugins_loaded', 'my_plugin_override');
function my_plugin_override() {
// your code here
if (isset($_POST['formSubmit']) && $_POST['formSubmit'] === 'true') {
global $wpdb;
$wpdb->query("UPDATE myTable set name='{$_POST['something']}' where id='{$_POST['id']}'");
header("Location: manageArticles?updated");
}
if (isset($_GET['updated'])) {
echo "you have updated the table!";
}
}
?>
<?php
//the rest of the code...
/** Step 2 (from text above). */
//......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.