skip to Main Content

I have run into a problem where my Script is loaded but it only gets executed when I am logged in. The role of the User does not matter.

It worked perfectly last week and as far as I know, there have been no changes to the site since then.

To test this, I tried to execute following code:

console.log('??????????')

There is no other code in the file.

Here are my results:

Not logged in:

I can find it in the ‘Network’ tab of the Chrome console

But there are no ‘??????’ in the console

Logged in as a normal User

I can still find it in the ‘Network’ tab

There are ‘??????’ in the console

How the script is enqueued:

function custom_scripts() {
    $rand = rand(1, 99999999);
   wp_enqueue_script(
     'custom', /* Name für das Script */
     get_stylesheet_directory_uri() . '/custom.js', /* Pfad */
     array( 'jquery' ), /* jQuery ist erforderlich */
     false,
     true
   );
     wp_enqueue_script( 'js4u', get_stylesheet_directory_uri() . '/js4u/js4u.min.js', array( 'jquery'), $rand, "all");

     if( is_page(8) ) {
         wp_enqueue_script( 'create-listing', get_stylesheet_directory_uri().'/js4u/create-listing.js',array(), $rand, "all");
     }

     if( is_page(13) ) {
         wp_enqueue_script( 'login-page', get_stylesheet_directory_uri().'/js4u/login-page.js',array(), $rand, "all");
     }

     if( is_page(14) ) {
         wp_enqueue_script( 'register-page', get_stylesheet_directory_uri().'/js4u/register-page.js',array(), $rand, "all");
     }

     wp_enqueue_script( 'primary-menu', get_stylesheet_directory_uri().'/js4u/primary-menu.js',array(), $rand, "all");

 }
 add_action( 'wp_enqueue_scripts', 'custom_scripts', 999 );

If I paste the code directly into the console, it works as intended.

I have tried it in Chrome, Firefox and Edge with the same results.

There is also no difference between Incognito and Normal mode.

This is the only file where this problem occurs, the others still work fine.

I hope you can help me 😀

Update: The alert and the console.log seem to work on every page except the front page. The rest of the code still doesn’t do anything.

Code that should be executed:

function fn() {
    let loginButton = document.querySelectorAll('#login-button-link')[1].parentNode;
    let usernameMenuItem = document.getElementById('menu-item-username');
    let firmaEintragen = document.getElementsByClassName('menu-item-8199')[1];
    let favorites = document.querySelectorAll('#menu-item-my-favorites')[1];

    /**
     * Entfernt den Eintrag 'Favorites' aus dem Username Dropdown im Header
     */
    if (usernameMenuItem){
        favorites.parentNode.removeChild(favorites);
        console.log('Username Dropdown angepasst');
    }

    /**
     * Entfernt den "Firma Eintragen" Button wenn der Login-Button sichtbar ist, der User also nicht eingeloggt ist.
     */
    let loginStyle = loginButton.style.display;
    if (!(loginStyle === 'none')) {
        firmaEintragen.style.display = 'none';
        console.log('Firma Eintragen entfernt');
    }
    if (loginStyle === 'none') {
        loginStyle = 'table-cell';
        console.log('Login geändert')
    }
}
fn();

2

Answers


  1. Chosen as BEST ANSWER

    Everything works as intended now.

    Big thanks to @jasie

    Turned $rand into a string with $rand = strval(rand(1, 99999999));.

    Changed $in_footer into true.

    Enabled caching plugin WP Rocket and cleared the cache a few times.

    Code in functions.php:

    function custom_scripts() {
        $rand = strval(rand(1, 99999999));
    
       wp_enqueue_script(
         'custom', /* Name für das Script */
         get_stylesheet_directory_uri() . '/custom.js', /* Pfad */
         array( 'jquery' ), /* jQuery ist erforderlich */
         false,
         true
       );
         wp_enqueue_script( 'js4u', get_stylesheet_directory_uri() . '/js4u/js4u.min.js', array( 'jquery'), $rand, true);
    
         if( is_page(8) ) {
             wp_enqueue_script( 'create-listing', get_stylesheet_directory_uri().'/js4u/create-listing.js',array(), $rand, true);
         }
    
         if( is_page(13) ) {
             wp_enqueue_script( 'login-page', get_stylesheet_directory_uri().'/js4u/login-page.js',array(), $rand, true);
         }
    
         if( is_page(14) ) {
             wp_enqueue_script( 'register-page', get_stylesheet_directory_uri().'/js4u/register-page.js',array(), $rand, true);
         }
    
         wp_enqueue_script( 'primary-menu', get_stylesheet_directory_uri().'/js4u/primary-menu.js', array('jquery'), $rand, true);
    
     }
     add_action( 'wp_enqueue_scripts', 'custom_scripts', 999 );
    

  2. Correct these two errors and then see, if it works:

    1. The last parameter of wp_enqueue_script() needs to be a Bool. You are passing a String ("all") which problably defaults to false (it should cause a PHP warning at least).
    2. The second last paramter needs to be a string, bool oder null. You are passing an Integer because that is what rand() return (it should also cause a PHP warning). Convert the int into string (e.g. through strval()).

    The $ver parameter is very caching relevant! Make sure you read the documentation properly: https://developer.wordpress.org/reference/functions/wp_enqueue_script/

    Also, I suggest you activate WP_DEBUG along with SCRIPT_DEBUG: https://wordpress.org/support/article/debugging-in-wordpress/#wp_debug

    Last advice: delete the browser cache properly (this can be done in the browser settings).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search