you 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;
}
}