• hi

    i wanna ask, im using WordPress and enable multisite, when i access /login or /admin or /dashboard it will be redirect to /wp-login.php, after i go deeper into wordpress code i found

    /**

     * Redirects a variety of shorthand URLs to the admin.

     *

     * If a user visits example.com/admin, they'll be redirected to /wp-admin.

     * Visiting /login redirects to /wp-login.php, and so on.

     *

     * @since 3.4.0

     *

     * @global WP_Rewrite $wp_rewrite WordPress rewrite component.

     */

    function wp_redirect_admin_locations() {

        global $wp_rewrite;

        if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) ) {

            return;

        }

        $admins = array(

            home_url( 'wp-admin', 'relative' ),

            home_url( 'dashboard', 'relative' ),

            home_url( 'admin', 'relative' ),

            site_url( 'dashboard', 'relative' ),

            site_url( 'admin', 'relative' ),

        );

        if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins, true ) ) {

            wp_redirect( admin_url() );

            exit;

        }

        $logins = array(

            home_url( 'wp-login.php', 'relative' ),

            home_url( 'login', 'relative' ),

            site_url( 'login', 'relative' ),

        );

        if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins, true ) ) {

            wp_redirect( wp_login_url() );

            exit;

        }

    }

    i dont want redirect thing, is it okay to make $admins = array() with empty value? or any ideas? thanks

    • This topic was modified 1 month, 3 weeks ago by Yui.
    • This topic was modified 1 month, 3 weeks ago by James Huff. Reason: moved to Netowkring WordPress since this is a multisite question
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Please do not alter any core code. Altering WP behavior must always be done via action and filter hooks. The function you found does not have any available hooks. However, you could use the “wp_redirect” filter to prevent redirects if $_SERVER['REQUEST_URI'] is any of the unwanted shorthand URLs. If your filter callback returns null it’ll disable the requested redirect.

    Thread Starter anugrahjaya1

    (@anugrahjaya1)

    hi bcworkz thanks for answering my question, but I think I forgot to mention one thing. the wp_redirect_admin_location is using on action templte_redirect, and I am aware to not change any WordPress core code, so my plan is to recreate the wp_redirect_admin_location with a new name and make it redirect to the home page if a user tried to access /login, /wp-admin, /dashboard, admin, and registered to the same action hook

    is that okay? Do you guys have any recommendations?

    Moderator bcworkz

    (@bcworkz)

    If you added a callback for “template_redirect” that redirects before wp_redirect_admin_locations() is called, that should work fine. There’s no problem with an approach like that.

    FYI, wp_redirect_admin_locations() is added to “template_redirect” with a priority of 1000, so a callback added with anything less than that should be effective. With such a large priority arg, it seems like the ability to override with an earlier hook was the intention all along.

    IMO, those admin requests going to home page is a bit odd. Personally, I’d rather such requests simply went 404. In which case I would simply use remove_action('template_redirect', 'wp_redirect_admin_locations', 1000 );. Ensuring this executes after WP actually adds the callback to start with of course.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.