Fix your Flash of Unstyled Content FOUC

Поделиться
HTML-код
  • Опубликовано: 23 дек 2024

Комментарии • 18

  • @shibupandit24
    @shibupandit24 3 месяца назад

    How to modify this code for bricks builder ?

    • @websquadron
      @websquadron  3 месяца назад

      Try this - but make a Backup first.
      add_action( 'wp_enqueue_scripts', function() {
      if ( ! did_action( 'bricks_enqueue_styles' ) ) {
      return;
      }
      // Replace this with your actual Bricks template ID
      $template_id = 4040;
      // Enqueue the CSS file associated with the Bricks template (custom logic here if needed)
      $css_file_url = get_template_directory_uri() . '/path-to-your-css-file.css';
      if ( file_exists( get_template_directory() . '/path-to-your-css-file.css' ) ) {
      wp_enqueue_style( 'custom-bricks-style', $css_file_url, [], filemtime( get_template_directory() . '/path-to-your-css-file.css' ) );
      }
      }, 500 );

  • @GhostiePostie
    @GhostiePostie 3 месяца назад +3

    I dont know if you had this one planned already or you actually listened to my previous comment, but thank you Imran! Much love for all you do in this space, cheers.

    • @websquadron
      @websquadron  3 месяца назад +2

      This was recorded 'months' ago :) :)

  • @hosseinkhanmohammadi4770
    @hosseinkhanmohammadi4770 3 месяца назад

    Hi Imran. Thanks for the video.
    I have an unrelated question. :))
    Do you prefer a curved or flat monitor for design & web design projects? 🤭

  • @Unfunctional
    @Unfunctional 3 месяца назад

    Hey, so if I add specific page ID (like in your code snippet) will it fix FOUC only for that page?

    • @websquadron
      @websquadron  3 месяца назад

      Yes. But you could add more. Though I would check as to why it keeps happening. Check your CSS styling or optimisation settings for CSS Minification or Aggregation.

  • @QuaverloveStudio
    @QuaverloveStudio 3 месяца назад

    Thanks for that, Imran.
    I added the code to Code Snippets, but I'm getting a syntax error on line 2: 'unexpected token "if"'.Searching around to find what the problem could be

    • @websquadron
      @websquadron  3 месяца назад +1

      Could be due to you using older PHP - maybe?
      Try this:
      function enqueue_elementor_template_css() {
      if ( ! class_exists( '\Elementor\Core\Files\CSS\Post' ) ) {
      return;
      }
      $template_id = 4040;
      $css_file = new \Elementor\Core\Files\CSS\Post( $template_id );
      $css_file->enqueue();
      }
      add_action( 'wp_enqueue_scripts', 'enqueue_elementor_template_css', 500 );

    • @QuaverloveStudio
      @QuaverloveStudio 3 месяца назад

      Soon as i get home, I'll have a look.
      Thanks again, Imran.

    • @QuaverloveStudio
      @QuaverloveStudio 3 месяца назад

      I checked and PHP had already been updated to 8.3 for that domain.
      So I tried the amended code you suggested. Not only did it not throw any errors, it works like a charm!
      The fact that you resorted to a plain ol' vanilla flavoured function makes me think it's an issue with ACF's JavaScript API and, as no one else has mentioned it, then it's an issue on my end.... that's a whole 'nother episode for another time.
      Thank you so much for your help, Imran.

  • @heavylog1c
    @heavylog1c 3 месяца назад

    This happens because WP allows plugins to embed code in footer. So it fires after the page has been loaded. Usually you need this feature to add some codes for Google Analytics in footer and stuff like that. But plugins developers also started to add their CSS and JS there and turned WP into mess. It's just a bad approach in developing imo and it should be stopped at some point. Everything should be in the head, especially CSS.

    • @websquadron
      @websquadron  3 месяца назад

      Totally agree. I do hate how many AI tools when creating codes recommend the footer placement.

  • @Hsabii222
    @Hsabii222 2 месяца назад

    Advanced script. What is your opinion?
    add_action( 'wp_head', 'fix_fouc' );
    function fix_fouc() {
    echo '
    body {
    visibility: hidden;
    }
    .loaded {
    visibility: visible;
    }
    ';
    }
    add_action( 'wp_footer', 'add_loaded_class' );
    function add_loaded_class() {
    echo '
    document.addEventListener("DOMContentLoaded", function() {
    document.body.classList.add("loaded");
    });
    ';
    }
    add_action( 'wp_enqueue_scripts', function() {
    if ( ! class_exists( '\Elementor\Core\Files\CSS\Post' ) ) {
    return;
    }
    $template_id = "id of template here";
    $css_file = new \Elementor\Core\Files\CSS\Post( $template_id );
    $css_file->enqueue();
    }, 500 );

    • @websquadron
      @websquadron  2 месяца назад +1

      I'll have to check

    • @Hsabii222
      @Hsabii222 2 месяца назад

      @@websquadron any updates?