0

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.

enter image description here

  • 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?

1
  • Why do you think this is a problem you need to solve? If they edit your domain they'd also go to the wrong page. If they changed /wp-admin to /ab-admin they would also get the wrong page. Feb 9 at 8:42

1 Answer 1

-1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.