Support » Fixing WordPress » How to load scripts on the Wordperss Login page?

  • Currently, I install Google Tag Manager by adding a script to header.php.

    However, is it possible to make this appear on the WordPress login screen of a website eg. site.com/wp-admin?

    I was told that I had to “Add the script to the relevant template.”

    Can someone guide me on how to do this?

    Thank you.

    • This topic was modified 6 months, 1 week ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Everything else WordPress topic
Viewing 5 replies - 1 through 5 (of 5 total)
  • try aomething like this

    function my_login_stylesheet() {
    if ( $GLOBALS[‘pagenow’] === ‘wp-login.php’ ) {
    // We’re on the login page!
    wp_enqueue_script( ‘custom-login’, get_stylesheet_directory_uri() . ‘/style-login.js’ );
    }

    }
    add_action( ‘login_enqueue_scripts’, ‘my_login_stylesheet’ );

    Note: get_stylesheet_directory_uri – This is for child theme

    Thread Starter jesseguttenberg

    (@jesseguttenberg)

    Hey @hemant-ahir, this is the script I need to insert. There are two parts to it. I don’t think I should be inserting it into a style sheet? Stylesheet sounds like CSS? Thank you.

    First part

    Paste this code as high in the <head> of the page as possible:
    <!-- Google Tag Manager -->
    <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
    new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
    j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
    'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
    })(window,document,'script','dataLayer','GTM-XXXXXX');</script>
    <!-- End Google Tag Manager -->

    Second part

    Additionally, paste this code immediately after the opening <body> tag:
    <!-- Google Tag Manager (noscript) -->
    <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXX"
    height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
    <!-- End Google Tag Manager (noscript) -->

    But why you need to load in wp-login.php file ? it should be always on frontside means in header or footer.php in your site.

    Thanks
    Ahir

    • This reply was modified 6 months, 1 week ago by Ahir Hemant.
    Thread Starter jesseguttenberg

    (@jesseguttenberg)

    @hemant-ahir thanks. That is because I want to use GTM to deploy a cookie for Internal users (ie. those who access the site.com/wp-admin page). That way I can easily separate out internal and external traffic. I am sure there are other ways to do this but I figured this is the most suitable for my current purposes.

    Can you help me out, please?

    Thank you.

    Ahir Hemant

    (@hemant-ahir)

    Okay fine.

    Please put your both scripts to custom-adminscript.js and put in your active theme,and put below function in function.php

    function my_login_stylesheet() {
    if ( $GLOBALS[‘pagenow’] === ‘wp-login.php’ ) {
    // We’re on the login page!
    wp_enqueue_script( ‘custom-adminscript’, get_stylesheet_directory_uri() . ‘/custom-adminscript.js’ );
    }

    }
    add_action( ‘login_enqueue_scripts’, ‘my_login_stylesheet’ );

    Please note: get_stylesheet_directory_uri() this is only for child theme.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to load scripts on the Wordperss Login page?’ is closed to new replies.