• I have some styles enqueued to be loaded on non-admin pages but they are also loaded on the login page which I don’t want. Is there any way to stop this and why does is_admin() return false on the login page, surely that doesn’t make sense.

Viewing 4 replies - 1 through 4 (of 4 total)
  • How are you enqueuing these styles?

    The login page returns false on is_admin() because, well, the login page isn’t the admin area. It’s a boundary/gateway between the front end and the back end.

    To detect the login page, you can use the $pagenow global, e.g.:

    global $pagenow;
    if ( 'wp-login.php' == $pagenow ) {
        // current page is the login page;
        // do something
    }

    So, for your script enqueue callback, simply change:

    if ( ! is_admin() )

    to:

    global $pagenow;
    if ( ! is_admin() && 'wp-login.php' != $pagenow )

    I like to take the route of hooking into template_redirect for scripts and styles I want to enqueue only on the front-end. Because template_redirect only fires when the front-end template is loaded, you’re guaranteed to be in the right place, and there’s no extra if statements or gotchas to remember.

    add_action('template_redirect', 'pd_frontend_enqueues');
    function pd_frontend_enqueues(){
      // enqueue scripts or styles here with wp_enqueue_script or wp_enqueue_style
      // see https://codex.www.ads-software.com/Function_Reference/wp_enqueue_script
      // wp_enqueue_script( 'script-name', get_template_directory_uri().'/js/script-file.js', array('jquery') );
    }

    Alternatively, for any readers wanting the opposite effect of enqueuing scripts in the admin, for plugins, it’s best practice to only enqueue on the specific plugin page needed. You can find an example of that in the codex under admin_enqueue_scripts > Example: Target a Specific Admin Page

    One more clarification: While my preference for frontend enqueue hook was “template_redirect” the Codex recommends “wp_enqueue_scripts”, which also only fires on the front-end.

    wp_enqueue_scripts would be considered “best practice,” since it’s generally better to attach to a later hook if possible.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘stop login page loading non admin styles’ is closed to new replies.