Git Product home page Git Product logo

Comments (5)

duracelltomi avatar duracelltomi commented on August 16, 2024 1

Here you are: f3fa210

from gtm4wp.

duracelltomi avatar duracelltomi commented on August 16, 2024

Script is loaded from here:
https://github.com/duracelltomi/gtm4wp/blob/master/js/gtm4wp-youtube.js

What I could imagine is to add a dom ready listener and then look at YT iframes in the DOM and only load this code if there are any YT embeds.

from gtm4wp.

morvy avatar morvy commented on August 16, 2024

I can imagine loading that file with something like this:

function gtm4wp_load_youtube_script() {
  global $post;
  if (has_block('core-embed/youtube', $post) || (strpos($post->post_content, '<iframe') !== false && strpos($post->post_content, 'youtu') !== false)) {
    wp_enqueue_script('gtm4wp-youtube');
  }
}
add_action('wp_enqueue_scripts', 'gtm4wp_load_youtube_script');

from gtm4wp.

duracelltomi avatar duracelltomi commented on August 16, 2024

I am not sure the performance of this code will be good. It scans through the whole post content on the server side increasing processing time and thus, response time.

Checking this in the browser seems to be a better solution for me.

from gtm4wp.

morvy avatar morvy commented on August 16, 2024

Yes this will definitely put some load on the server, but I believe every WP site now uses some kind of caching - if not, PSI is looking bad and those numbers matter to managers :) So if it's checked on server and then saved in cache in pure html (W3TC, Super Cache, WP Rocket..) it shouldn't add load on server anymore.
I don't like additional scripts because JS is still single threaded and it can block execution of other JS. I'm pretty bad in JS, so I just avoid using JS whenever possible, because when I use it, it has impact on performance - mostly on mobiles.

from gtm4wp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.