admin管理员组文章数量:1125389
I created a custom settings page like below.
add_menu_page(
__('Management', 'domain'),
__('Management', 'domain'),
'activate_plugins',
'management', // <---
function () {
echo 'Hello';
},
'icon',
999
);
As we can see, this page is accessed via the "admin.php?page=management
" link. I commented above with "<---
"
But this is such a page that users can change this slug.
- For example, let's make this "
management-2
" and save the form. - In this case, WordPress redirects to "
admin.php?page=management&settings-updated=true
", but since we set it to "management-2
", I get a "403
" error. - At this point I need to catch a hook, start a new redirect and return it to the "
admin.php?page=management-2&settings-updated=true
" link, but I couldn't find any hook to do this.
Does anyone have any solution suggestions?
I created a custom settings page like below.
add_menu_page(
__('Management', 'domain'),
__('Management', 'domain'),
'activate_plugins',
'management', // <---
function () {
echo 'Hello';
},
'icon',
999
);
As we can see, this page is accessed via the "admin.php?page=management
" link. I commented above with "<---
"
But this is such a page that users can change this slug.
- For example, let's make this "
management-2
" and save the form. - In this case, WordPress redirects to "
admin.php?page=management&settings-updated=true
", but since we set it to "management-2
", I get a "403
" error. - At this point I need to catch a hook, start a new redirect and return it to the "
admin.php?page=management-2&settings-updated=true
" link, but I couldn't find any hook to do this.
Does anyone have any solution suggestions?
Share Improve this question asked Feb 9, 2024 at 6:13 G.D.iG.D.i 235 bronze badges 1 |1 Answer
Reset to default -1you can use the admin_init
hook along with the add_query_arg()
function to modify the redirection URL for handle the situation.
Redirect users to the correct URL admin.php?page=management
if they access the settings page with an unexpected slug.
Modify the redirection URL after settings have been updated to include the current page slug management
, ensuring that users have redirected back to the correct page admin.php?page=management&settings-updated=true
add_action('admin_init', 'check_custom_settings_page_redirect');
function check_custom_settings_page_redirect() {
// Check if we are on the settings page and it's not the expected slug
if (isset($_GET['page']) && $_GET['page'] !== 'management') {
// Redirect to the correct URL with the updated slug
$redirect_url = admin_url('admin.php?page=management');
wp_safe_redirect($redirect_url);
exit;
}
// Check if settings have been updated
if (isset($_GET['settings-updated']) && $_GET['settings-updated'] === 'true') {
// Modify the redirect URL to include the current page slug
$redirect_url = add_query_arg(array('page' => 'management'), admin_url('admin.php'));
wp_safe_redirect($redirect_url);
exit;
}
}
本文标签: phpHow can I catch WordPress custom settings page slug has already changed
版权声明:本文标题:php - How can I catch WordPress custom settings page slug has already changed? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736657490a1946292.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
/wp-admin
to/ab-admin
they would also get the wrong page. – Jacob Peattie Commented Feb 9, 2024 at 8:42