Support » Fixing WordPress » How To Place Js In My Child Theme Functions File?

  • Resolved nootkan

    (@nootkan)


    Hi guys, been trying to figure out how to use js to defer my css inside my child theme functions file so I don’t have to use a plugin. Here is what I’ve tried based on other solutions in this forum but it isn’t working. Any ideas?

    /* Defer Js and Css */
    function prefix_footer_code() {
    <script>
          var cb = function() {
            var l = document.createElement('link'); l.rel = 'stylesheet';
            l.href = 'wp-content/uploads/omgf/shapla-fonts/shapla-fonts.css?ver=1649559414';
            var h = document.getElementsByTagName('head')[0]; h.parentNode.insertBefore(l, h);
          };
          var raf = requestAnimationFrame || mozRequestAnimationFrame ||
              webkitRequestAnimationFrame || msRequestAnimationFrame;
          if (raf) raf(cb);
          else window.addEventListener('load', cb);
        </script>
    <script>
          var cb = function() {
            var l = document.createElement('link'); l.rel = 'stylesheet';
            l.href = 'wp-content/themes/shapla/assets/font-awesome/css/all.min.css?ver=5.5.0';
            var h = document.getElementsByTagName('head')[0]; h.parentNode.insertBefore(l, h);
          };
          var raf = requestAnimationFrame || mozRequestAnimationFrame ||
              webkitRequestAnimationFrame || msRequestAnimationFrame;
          if (raf) raf(cb);
          else window.addEventListener('load', cb);
        </script>
    <script>
          var cb = function() {
            var l = document.createElement('link'); l.rel = 'stylesheet';
            l.href = 'wp-content/themes/shapla/assets/css/main.css?ver=2.0.1';
            var h = document.getElementsByTagName('head')[0]; h.parentNode.insertBefore(l, h);
          };
          var raf = requestAnimationFrame || mozRequestAnimationFrame ||
              webkitRequestAnimationFrame || msRequestAnimationFrame;
          if (raf) raf(cb);
          else window.addEventListener('load', cb);
        </script>
    <script>
          var cb = function() {
            var l = document.createElement('link'); l.rel = 'stylesheet';
            l.href = 'wp-content/themes/shapla/style.css?ver=5.9.3';
            var h = document.getElementsByTagName('head')[0]; h.parentNode.insertBefore(l, h);
          };
          var raf = requestAnimationFrame || mozRequestAnimationFrame ||
              webkitRequestAnimationFrame || msRequestAnimationFrame;
          if (raf) raf(cb);
          else window.addEventListener('load', cb);
        </script>
    <script>
          var cb = function() {
            var l = document.createElement('link'); l.rel = 'stylesheet';
            l.href = 'wp-content/uploads/shapla/css/customize-style-6253133c6502a.css?ver=1649611580';
            var h = document.getElementsByTagName('head')[0]; h.parentNode.insertBefore(l, h);
          };
          var raf = requestAnimationFrame || mozRequestAnimationFrame ||
              webkitRequestAnimationFrame || msRequestAnimationFrame;
          if (raf) raf(cb);
          else window.addEventListener('load', cb);
        </script>
    <script>
          var cb = function() {
            var l = document.createElement('link'); l.rel = 'stylesheet';
            l.href = 'wp-content/themes/shapla-child/style.css?ver=5.9.3';
            var h = document.getElementsByTagName('head')[0]; h.parentNode.insertBefore(l, h);
          };
          var raf = requestAnimationFrame || mozRequestAnimationFrame ||
              webkitRequestAnimationFrame || msRequestAnimationFrame;
          if (raf) raf(cb);
          else window.addEventListener('load', cb);
        </script>
    }
    add_action( 'wp_footer', 'prefix_footer_code' );
    • This topic was modified 10 months ago by t-p. Reason: Moved to Fixing WordPress from Everything else WordPress
Viewing 5 replies - 1 through 5 (of 5 total)
  • threadi

    (@threadi)

    You forgot to write where PHP ends and where it starts again in your code. Therefore, this will probably cause a PHP error when calling your site.

    You would have to adjust it like this:

    /* Defer Js and Css */
    function prefix_footer_code() {
     ?>
    <script>
          var cb = function() {
            var l = document.createElement('link'); l.rel = 'style sheet';
            l.href = 'wp-content/uploads/omgf/shapla-fonts/shapla-fonts.css?ver=1649559414';
            var h = document.getElementsByTagName('head')[0]; h.parentNode.insertBefore(l, h);
          };
          var raf = requestAnimationFrame || mozRequestAnimationFrame ||
              webkitRequestAnimationFrame || msRequestAnimationFrame;
          if (raf) raf(cb);
          else window.addEventListener('load', cb);
        </script>
    <script>
          var cb = function() {
            var l = document.createElement('link'); l.rel = 'style sheet';
            l.href = 'wp-content/themes/shapla/assets/font-awesome/css/all.min.css?ver=5.5.0';
            var h = document.getElementsByTagName('head')[0]; h.parentNode.insertBefore(l, h);
          };
          var raf = requestAnimationFrame || mozRequestAnimationFrame ||
              webkitRequestAnimationFrame || msRequestAnimationFrame;
          if (raf) raf(cb);
          else window.addEventListener('load', cb);
        </script>
    <script>
          var cb = function() {
            var l = document.createElement('link'); l.rel = 'style sheet';
            l.href = 'wp-content/themes/shapla/assets/css/main.css?ver=2.0.1';
            var h = document.getElementsByTagName('head')[0]; h.parentNode.insertBefore(l, h);
          };
          var raf = requestAnimationFrame || mozRequestAnimationFrame ||
              webkitRequestAnimationFrame || msRequestAnimationFrame;
          if (raf) raf(cb);
          else window.addEventListener('load', cb);
        </script>
    <script>
          var cb = function() {
            var l = document.createElement('link'); l.rel = 'style sheet';
            l.href = 'wp-content/themes/shapla/style.css?ver=5.9.3';
            var h = document.getElementsByTagName('head')[0]; h.parentNode.insertBefore(l, h);
          };
          var raf = requestAnimationFrame || mozRequestAnimationFrame ||
              webkitRequestAnimationFrame || msRequestAnimationFrame;
          if (raf) raf(cb);
          else window.addEventListener('load', cb);
        </script>
    <script>
          var cb = function() {
            var l = document.createElement('link'); l.rel = 'style sheet';
            l.href = 'wp-content/uploads/shapla/css/customize-style-6253133c6502a.css?ver=1649611580';
            var h = document.getElementsByTagName('head')[0]; h.parentNode.insertBefore(l, h);
          };
          var raf = requestAnimationFrame || mozRequestAnimationFrame ||
              webkitRequestAnimationFrame || msRequestAnimationFrame;
          if (raf) raf(cb);
          else window.addEventListener('load', cb);
        </script>
    <script>
          var cb = function() {
            var l = document.createElement('link'); l.rel = 'style sheet';
            l.href = 'wp-content/themes/shapla-child/style.css?ver=5.9.3';
            var h = document.getElementsByTagName('head')[0]; h.parentNode.insertBefore(l, h);
          };
          var raf = requestAnimationFrame || mozRequestAnimationFrame ||
              webkitRequestAnimationFrame || msRequestAnimationFrame;
          if (raf) raf(cb);
          else window.addEventListener('load', cb);
        </script>
    <?php
    }
    add_action( 'wp_footer', 'prefix_footer_code' );

    But I’m also wondering what that’s supposed to be. If you only want to integrate your own JavaScript files, I would recommend using wp_enqueue_scripts to solve this. An example:

    function wpb_adding_scripts() {
    wp_register_script('my_amazing_script', get_template_directory_uri() . '/js/amazing_script.js', array('jquery'),'1.1', true);
    wp_enqueue_script('my_amazing_script');
    }
    add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );

    Source: https://www.wpbeginner.com/wp-tutorials/how-to-properly-add-javascripts-and-styles-in-wordpress/

    The procedure is also described here: https://wpmudev.com/blog/adding-scripts-and-styles-wordpress-enqueueing/

    Thread Starter nootkan

    (@nootkan)

    threadi, thanks for your reply.

    The snippet (which I didn’t write but found on another website) I am trying place in the functions file is supposed to defer css and fix my rendering issues with page speed insights.

    It works fine on an html page I created but my lack of php skills is making it tough to work properly in wordpress.

    I’ve tried three different popular plugins but each one lowered my performance score from 96 to 78 so I wanted to see if I could just add a snippet in the child theme function file to defer the css and fix my rendering issue which should bring up my performance score.

    I tried your example with this but you were right it caused a critical error:

    function wpb_adding_scripts() {
    wp_register_script('my_amazing_script', get_template_directory_uri() . '/js/amazing_script.js', array('jquery'),'1.1', true);
    wp_enqueue_script('<script>
          var cb = function() {
            var l = document.createElement('link'); l.rel = 'stylesheet';
            l.href = 'wp-content/uploads/omgf/shapla-fonts/shapla-fonts.css?ver=1649559414';
            var h = document.getElementsByTagName('head')[0]; h.parentNode.insertBefore(l, h);
          };
          var raf = requestAnimationFrame || mozRequestAnimationFrame ||
              webkitRequestAnimationFrame || msRequestAnimationFrame;
          if (raf) raf(cb);
          else window.addEventListener('load', cb);
        </script>
    <script>
          var cb = function() {
            var l = document.createElement('link'); l.rel = 'stylesheet';
            l.href = 'wp-content/themes/shapla/assets/font-awesome/css/all.min.css?ver=5.5.0';
            var h = document.getElementsByTagName('head')[0]; h.parentNode.insertBefore(l, h);
          };
          var raf = requestAnimationFrame || mozRequestAnimationFrame ||
              webkitRequestAnimationFrame || msRequestAnimationFrame;
          if (raf) raf(cb);
          else window.addEventListener('load', cb);
        </script>
    <script>
          var cb = function() {
            var l = document.createElement('link'); l.rel = 'stylesheet';
            l.href = 'wp-content/themes/shapla/assets/css/main.css?ver=2.0.1';
            var h = document.getElementsByTagName('head')[0]; h.parentNode.insertBefore(l, h);
          };
          var raf = requestAnimationFrame || mozRequestAnimationFrame ||
              webkitRequestAnimationFrame || msRequestAnimationFrame;
          if (raf) raf(cb);
          else window.addEventListener('load', cb);
        </script>
    <script>
          var cb = function() {
            var l = document.createElement('link'); l.rel = 'stylesheet';
            l.href = 'wp-content/themes/shapla/style.css?ver=5.9.3';
            var h = document.getElementsByTagName('head')[0]; h.parentNode.insertBefore(l, h);
          };
          var raf = requestAnimationFrame || mozRequestAnimationFrame ||
              webkitRequestAnimationFrame || msRequestAnimationFrame;
          if (raf) raf(cb);
          else window.addEventListener('load', cb);
        </script>
    <script>
          var cb = function() {
            var l = document.createElement('link'); l.rel = 'stylesheet';
            l.href = 'wp-content/uploads/shapla/css/customize-style-6253133c6502a.css?ver=1649611580';
            var h = document.getElementsByTagName('head')[0]; h.parentNode.insertBefore(l, h);
          };
          var raf = requestAnimationFrame || mozRequestAnimationFrame ||
              webkitRequestAnimationFrame || msRequestAnimationFrame;
          if (raf) raf(cb);
          else window.addEventListener('load', cb);
        </script>
    <script>
          var cb = function() {
            var l = document.createElement('link'); l.rel = 'stylesheet';
            l.href = 'wp-content/themes/shapla-child/style.css?ver=5.9.3';
            var h = document.getElementsByTagName('head')[0]; h.parentNode.insertBefore(l, h);
          };
          var raf = requestAnimationFrame || mozRequestAnimationFrame ||
              webkitRequestAnimationFrame || msRequestAnimationFrame;
          if (raf) raf(cb);
          else window.addEventListener('load', cb);
        </script>');
    }
    
    add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );

    Also took the js code snippet and placed it inside an external js file and tried calling it this way but that didn’t work either.

    function wpb_adding_scripts() {
    wp_register_script('defer-css-js.js', get_template_directory_uri() . '/js/amazing_script.js', array('jquery'),'1.1', true);
    wp_enqueue_script('defer-css-js.js');
    }
    add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );
    • This reply was modified 10 months ago by nootkan.
    • This reply was modified 10 months ago by nootkan.
    • This reply was modified 10 months ago by nootkan.
    • This reply was modified 10 months ago by nootkan.

    Please see the manual at https://developer.wordpress.org/reference/functions/wp_enqueue_script/. According to it you are not allowed to insert inline scripts like you are trying to do. For that there is this function: https://developer.wordpress.org/reference/functions/wp_add_inline_script/

    In your 2nd attempt I suspect that you have HTML code in the JavaScript file, which would also be wrong.

    Maybe you should have a look at some more basics before you tinker further or use one of the established optimization plugins. You may have to configure them correctly, i.e. according to the theme, and not just activate them. If you have problems with them, ask in their support forums.

    Your scripts should be in an external file. like script1.js, script2.js. You can place both scripts in the same file. Do not add the <script> tags to the file they are not needed. You will need to save the file inside either your theme folder or a Plugin.

    You can then enqueue the script using the file path.

    You can use either https://developer.wordpress.org/reference/functions/get_template_directory_uri/ or
    https://developer.wordpress.org/reference/functions/plugin_dir_url/ to determine the path to your file
    example path would be plugin_dir_url (). “/myplugin/scripts/script1.js”

    • This reply was modified 9 months, 4 weeks ago by mrtom414.
    Thread Starter nootkan

    (@nootkan)

    Thanks threadi and mrtom414, between the two of you I was able to get something to work however when I checked my page speed insights again the page speed time had increased even though the render blocking issues had disappeared.

    So I am going to stick with what I have configured for now (98 and all green) and leave well enough alone.

    Appreciate all your support.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How To Place Js In My Child Theme Functions File?’ is closed to new replies.