php 如何在 Wordpress 中重定向到不同的管理页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/495478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to redirect to different admin page in Wordpress?
提问by dr_bonzo
I am writing a Wordpress plugin.
我正在编写一个 Wordpress 插件。
I want to perform a redirect (after creating DB records from POST data, etc...) to other ADMIN page.
我想执行重定向(在从 POST 数据等创建数据库记录之后...)到其他 ADMIN 页面。
Neither header("Location: ...) nor wp_redirect() work - i get
header("Location: ...) 和 wp_redirect() 都不起作用 - 我明白了
Warning: Cannot modify header information - headers already sent by
警告:无法修改标头信息 - 标头已由
which comes from obvious reason.
这来自明显的原因。
How do I properly perform a redirect in a Wordpress?
如何在 Wordpress 中正确执行重定向?
回答by codecowboy
On your form action, add 'noheader=true' to the action URL. This will prevent the headers for the admin area from being outputted before your redirect. For example:
在您的表单操作上,将“noheader=true”添加到操作 URL。这将防止在重定向之前输出管理区域的标题。例如:
<form name="post" action="<?php echo admin_url('admin.php?page=your-admin-page&noheader=true'); ?>" method="post" id="post">
回答by zero_padded
If you still want to redirect from your plugin admin page to another admin page while using WP add_page* functions then, after processing your request, you can just echo something like this:
如果您在使用 WP add_page* 函数时仍想从您的插件管理页面重定向到另一个管理页面,那么在处理您的请求后,您可以回显如下内容:
<script type="text/javascript">
window.location = '/whatever_page.php';
</script>
This just renders a javascript based redirect to "/whatever_page.php" thus ensuring no trouble with headers already sent by WP as Chris Ballance already said.
这只是将基于 javascript 的重定向呈现到“/whatever_page.php”,从而确保 WP 已经发送的标头不会出现问题,正如 Chris Ballance 已经说过的那样。
Change "/whatever_page.php" to something like "/wp-admin/admin.php?page=whatever_page"
将“/whatever_page.php”更改为“/wp-admin/admin.php?page=whatever_page”
回答by WraithKenny
For a link added with add_submenu_page (or related function), use the returned $hook_suffix to add an action to "load-$hook_suffix" and do the redirect there. This is how you hook to the page load before the output has begun.
对于使用 add_submenu_page(或相关函数)添加的链接,使用返回的 $hook_suffix 将操作添加到“load-$hook_suffix”并在那里进行重定向。这就是在输出开始之前挂钩页面加载的方式。
回答by dr_bonzo
I think I was doing it the wrong way.
我想我做错了。
My code was inside a add_menu_page() inside add_action('admin_menu', ...) call
我的代码在 add_action('admin_menu', ...) 调用中的 add_menu_page() 中
which is probably called later during the request (after page header has been created and displayed).
这可能会在请求期间稍后调用(在创建和显示页眉之后)。
Moving my code outside of my plugin handles, into main scope worked - it needs some cleanup, and fixes, but redirect works.
将我的代码移出我的插件句柄,进入主范围工作 - 它需要一些清理和修复,但重定向有效。
Anyway, thanks for the answers.
无论如何,感谢您的回答。
回答by Chris Ballance
You need to make sure that nothing is sent to http output before the redirect takes place.
您需要确保在重定向发生之前没有任何内容发送到 http 输出。
You can set "window.location('newlocation');" and that will still let you redirect after output has been sent to the browser.
您可以设置“window.location('newlocation');” 这仍然可以让您在输出发送到浏览器后重定向。
回答by SilentGhost
I suppose you just have to make sure that wp_redirect() comes before any output has been sent.
我想您只需要确保 wp_redirect() 在发送任何输出之前出现。
回答by Andy
Load it into template_redirect.
将其加载到 template_redirect 中。
add_action('template_redirect', 'myplugin_template_redirect');
function myplugin_template_redirect() {
wp_redirect('http://www.example.com/', 301);
}

