WordPress.org

Ready to get started?Download WordPress

Forums

stop login page loading non admin styles (5 posts)

  1. neonWired
    Member
    Posted 2 years ago #

    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.

  2. esmi
    Forum Moderator
    Posted 2 years ago #

    How are you enqueuing these styles?

  3. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    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 )
  4. Paul Clark
    Member
    Posted 2 years ago #

    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 http://codex.wordpress.org/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

  5. Paul Clark
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic