[英]How to redirect to different admin page in Wordpress?
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数据创建DB记录等之后......)到其他ADMIN页面。
Neither header("Location: ...) nor wp_redirect() work - i get 标题(“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中正确执行重定向?
On your form action, add 'noheader=true' to the action URL. 在表单操作上,将“noheader = true”添加到操作URL。 This will prevent the headers for the admin area from being outputted before your redirect.
这样可以防止在重定向之前输出管理区域的标题。 For example:
例如:
<form name="post" action="<?php echo admin_url('admin.php?page=your-admin-page&noheader=true'); ?>" method="post" id="post">
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”
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. 对于添加了add_submenu_page(或相关函数)的链接,请使用返回的$ hook_suffix向“load- $ hook_suffix”添加操作并在那里进行重定向。 This is how you hook to the page load before the output has begun.
这是在输出开始之前挂钩到页面加载的方式。
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. 无论如何,谢谢你的答案。
You need to make sure that nothing is sent to http output before the redirect takes place. 您需要确保在重定向发生之前没有任何内容发送到http输出。
You can set "window.location('newlocation');" 你可以设置“window.location('newlocation');” and that will still let you redirect after output has been sent to the browser.
并且在输出发送到浏览器后仍然可以重定向。
我想你必须确保wp_redirect()在任何输出发送之前出现。
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.