繁体   English   中英

如何在Wordpress管理页面中设置PHP标头重定向

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM