• Hi there,

    I have a chat installed on a website (here) – not working on chrome and firefox. It supposed to be visible on every page, but it shows up only in home.

    This is how I added the script in theme’s functions:

    function add_chat ( ) { 
         ?>
                <script type="text/javascript">
                SERVICE_PATTERN_CHAT_CONFIG = {
                    appId: ###
                    clientId: ###
                    apiUrl: ###
                    tenantUrl: ###
                    width: 300,
                    chatPath: ''
    
                };
                </script>
                <script type="text/javascript" src="/js/snippet.js"></script>
        <?php
           }
    
    add_action ('wp_footer', 'add_chat' );

    I also tried the wp_enqueue down there, but I had no luck.

    function add_chat_scripts() {

    `wp_register_script( ‘chat’, get_stylesheet_directory_uri() . ‘/js/chat.js’ , array(), true );
    wp_register_script( ‘chat-snippet’, get_stylesheet_directory_uri() . ‘/js/snippet.js’, array(‘chat’) );
    wp_enqueue_script (‘chat’);
    wp_enqueue_script(‘chat-snippet’);
    }
    add_action( ‘wp_enqueue_scripts’, ‘add_chat_scripts’ );`
    `

    Any help?

    Thank you

    • This topic was modified 7 years, 9 months ago by layza.
    • This topic was modified 7 years, 9 months ago by layza.
    • This topic was modified 7 years, 9 months ago by layza.
Viewing 15 replies - 1 through 15 (of 28 total)
  • Moderator bcworkz

    (@bcworkz)

    While there are a few exceptions, generally speaking, never use relative file references in WP. Because of how variable permalinks are, relative references are bound to fail somewhere. Always use absolute references by using functions like get_stylesheet_directory_uri().

    Enqueuing scripts is generally preferable to <script> tags. Did you try to use wp_enqueue_script() in footer code? That is too late. Such code should be in functions.php.

    Thread Starter layza

    (@layza)

    Thanks for replying.

    I used the enqueu in functions, I am notte sure what you meam. But i have the sane problem. Working in homepage and not working in other pages.

    Moderator bcworkz

    (@bcworkz)

    In your add_chat() function, the src attribute should be more like this:
    src="<?php echo get_stylesheet_directory_uri(); ?>/js/snippet.js"

    After this change, verify the resulting link in your page’s HTML source is correct. Not only on the home page, but other pages as well.

    If the script appears correctly on all pages, the fault might be in the chat script itself. That would need to be resolved by the chat service provider.

    Thread Starter layza

    (@layza)

    Thanks @bcworkz, I tried what you said but it’s still not working.

    In homepage the path is <script src="https://becloudsolutions.com/wp-content/themes/thekeynote-v1-07/js/snippet.js" type="text/javascript"></script>

    While in all other pages appears to be:
    <script src="js/snippet.js" type="text/javascript"></script>

    How it can be?

    By the way, I also tried this in functions.php but it gave me the same problem. ??

    function add_chat_scripts() {
       if ( is_page ()){
        
        wp_register_script( 'chat', get_stylesheet_directory_uri() . '/js/chat.js' , array(), true );
        wp_register_script( 'chat-snippet', get_stylesheet_directory_uri() . '/js/snippet.js', array('chat') );
       wp_enqueue_script  ('chat');
        wp_enqueue_script('chat-snippet');
       }
    }
       add_action( 'wp_enqueue_scripts', 'add_chat_scripts' );
    

    Tnx

    • This reply was modified 7 years, 9 months ago by layza.
    Moderator bcworkz

    (@bcworkz)

    So apparently get_stylesheet_directory_uri() fails on non-home pages. That’s really odd. Other code can modify the return of this function, so something hooked into one of the involved filters can be improperly modifying the result. We should try to identify the source of this problem.

    Deactivate all of your plugins and reload a non-homepage, then check the HTML source for a proper link. If it’s still wrong with all plugins deactivated, the problem is your theme. Assuming the links are now correct, reactivate plugins one by one, checking non-home links each time. Once the link fails to be complete again, the last activated plugin is at fault.

    Restore all the plugins you normally use except the one that messes up the directory path. See if your chat app works under this condition.

    Thread Starter layza

    (@layza)

    Hi there (@bcworkz)

    thanks. I haven’t try deactivation plugins yet, but now if I try to enter the backend I get this error:

    Warning: Cannot modify header information - headers already sent by (output started at /home/content/n3pnexwpnas02_data01/42/2910242/html/wp-content/themes/thekeynote-v1-07/functions.php:151) in /home/content/n3pnexwpnas02_data01/42/2910242/html/wp-includes/pluggable.php on line 1179

    I checked the code, and apperntly there aren’t empty space or strange thing in the functions.php.

    Moderator bcworkz

    (@bcworkz)

    Something is causing output of some sort. What’s at line 151 of /thekeynote-v1-07/functions.php? Any functions near this area that might cause output?

    Thread Starter layza

    (@layza)

    At line 151 there’s nothing, but I guess that was the problem. I deleted every empty space after the end of the code and now I don’t get that error anymore.

    Also, I tried to deactivate all the plugins, and the path is now correct in non-homepage pages too, but I still can’t see the chat. :/

    • This reply was modified 7 years, 9 months ago by layza.
    Thread Starter layza

    (@layza)

    Also, I just realized that on non-homepage pages there are a lot of syntax error, also for plugins and stuff it’s not in chat js folder.
    Image and video hosting by TinyPic

    https://i63.tinypic.com/2zdt5ab.jpg

    • This reply was modified 7 years, 9 months ago by layza.
    • This reply was modified 7 years, 9 months ago by layza.
    • This reply was modified 7 years, 9 months ago by layza.
    • This reply was modified 7 years, 9 months ago by layza.
    Moderator bcworkz

    (@bcworkz)

    Yikes! That would explain why chat is failing – JS gave up running anything after all that. Most of those JS modules that are shown in your image are mostly well established modules, so there shouldn’t be any errors within them, rather something else is using them improperly, which causes the errors.

    Is this also with all plugins deactivated? If so then something is wrong with your theme’s use of JS/jQuery. Otherwise, try the deactivation thing again to at least narrow down the error source to a particular plugin. If you can get rid of all of the errors, the chat should at least work.

    Thread Starter layza

    (@layza)

    Ok, now I don’t have any js error in the console except 2 for the chat.

    The server has not found anything matching the requested URI (Uniform Resource Identifier).

    Apparently is not able to get https://becloudsolutions.com/resources/js/json2.min.js and https://becloudsolutions.com/resources/js/jquery-1.11.0.min.js.

    Moderator bcworkz

    (@bcworkz)

    It appears whatever script is trying to load these is using the current URL instead of the proper one. If you are enqueuing your chat files, to get these files loaded you merely need to reference them as dependencies when you enqueue. array('jquery','json2')

    Then you can comment out whatever code is trying to load these resources with the wrong URL

    Thread Starter layza

    (@layza)

    Ok, thank you. So, now my function it like this, but it doesn’t get the json and jquery anyway.

    //CHAT SNIPPET
        function add_chat_scripts() {
            wp_register_script ('jquery', get_template_directory_uri() . '/js/jquery-1.11.0.min.js', array(), true );
            wp_enqueue_script  ('jquery');
            wp_register_script ('json', get_template_directory_uri() . '/js/json2.min.js', array(), true );
            wp_enqueue_script  ('json');
            wp_register_script( 'chat', get_template_directory_uri() . '/js/chat.js' , array(), true );
            wp_enqueue_script  ('chat');
            wp_register_script( 'chat-snippet', get_template_directory_uri() . '/js/snippet.js', array('chat', 'json', 'jquery'));
            wp_enqueue_script('chat-snippet');
           }
            add_action( 'wp_enqueue_scripts', 'add_chat_scripts' );

    What have i done wrong?

    Moderator bcworkz

    (@bcworkz)

    You are registering jquery when it is already pre-registered as a different file, causing conflicts. The json2 library is also pre-registered, but as ‘json2‘, not just ‘json’.

    More information here, including the following core-registered scripts section.

    Thread Starter layza

    (@layza)

    Ah…oops!

    So, the correct function would be something like:

    //CHAT SNIPPET
        function add_chat_scripts() {
            wp_enqueue_script  ('jquery');
            wp_enqueue_script  ('json2');
            wp_register_script( 'chat', get_template_directory_uri() . '/js/chat.js' , array(), true );
            wp_enqueue_script  ('chat');
            wp_register_script( 'chat-snippet', get_template_directory_uri() . '/js/snippet.js', array('chat', 'json2', 'jquery'));
            wp_enqueue_script('chat-snippet');
           }
            add_action( 'wp_enqueue_scripts', 'add_chat_scripts' );

    Or to put them in the array of (‘chat’) would be enough?

    Tnx

Viewing 15 replies - 1 through 15 (of 28 total)
  • The topic ‘Snippet in all pages’ is closed to new replies.