如果用户访问另一个管理页面,是否可以将用户重定向到管理页面?
例如,如果用户点击“所有页面”/wp-admin/edit.php?post_type=page
他们将被重定向到“添加新页面”/wp-admin/post-new.php?post_type=page
如果用户访问另一个管理页面,是否可以将用户重定向到管理页面?
例如,如果用户点击“所有页面”/wp-admin/edit.php?post_type=page
他们将被重定向到“添加新页面”/wp-admin/post-new.php?post_type=page
/**
* Redirect admin pages.
*
* Redirect specific admin page to another specific admin page.
*
* @return void
* @author Michael Ecklund
*
*/
function disallowed_admin_pages() {
global $pagenow;
# Check current admin page.
if ( $pagenow == \'edit.php\' && isset( $_GET[\'post_type\'] ) && $_GET[\'post_type\'] == \'page\' ) {
wp_redirect( admin_url( \'/post-new.php?post_type=page\' ) );
exit;
}
}
Fire the above function on the hook admin_init
.
add_action( \'admin_init\', \'disallowed_admin_pages\' );
Alternate syntax:
/**
* Redirect admin pages.
*
* Redirect specific admin page to another specific admin page.
*
* @return void
* @author Michael Ecklund
*
*/
add_action( \'admin_init\', function () {
global $pagenow;
# Check current admin page.
if ( $pagenow == \'edit.php\' && isset( $_GET[\'post_type\'] ) && $_GET[\'post_type\'] == \'page\' ) {
wp_redirect( admin_url( \'/post-new.php?post_type=page\' ) );
exit;
}
} );
Michael的解决方案似乎是为在类中使用而设计的,因此对于任何想要直接在函数中工作的独立函数的人来说。下面的示例包括来自customize的重定向。php到一个主题选项页面和一个来自Michael原始函数的页面。
function admin_redirects() {
global $pagenow;
/* Redirect Customizer to Theme options */
if($pagenow == \'customize.php\'){
wp_redirect(admin_url(\'/admin.php?page=theme_options\', \'http\'), 301);
exit;
}
/* OP\'s redirect from /wp-admin/edit.php?post_type=page */
if($pagenow == \'edit.php\' && isset($_GET[\'post_type\']) && $_GET[\'post_type\'] == \'page\'){
wp_redirect(admin_url(\'/post-new.php?post_type=page\', \'http\'), 301);
exit;
}
}
add_action(\'admin_init\', \'admin_redirects\');
是的,这可以通过添加action 到admin_init
, 此时,您可以检查请求uri以查看它是否匹配/wp-admin/edit.php?post_type=page
如果它确实重定向到添加帖子页面:/wp-admin/post-new.php?post_type=page
.
还有Plugin API 以及action reference WordPress codex上的页面详细介绍了操作及其工作方式。
我在添加时遇到了一些问题jquery-week-calendar 到WordPress的管理界面,使用add_menu_page 以及其他API方法。我发现默认值jquery-ui 和其他JavaScript引用正在干扰呈现的日历行为。如何从管理界面中删除这些额外脚本?load-scripts.php 调用了几次,我似乎无法注销tinyMCE等管理组件调用的脚本。