0

I found a CSS animation snippet on Codepen, and would like to add it in my WordPress site for preloader animation.

I have seen similar question being asked on here , but I couldn't resolve this from the available information there. I want to know:

1) In which location should I add the codepen html code?

2) In which location should I add given CSS?

3) And in which location should I add any required JS to make it work like it happens here?

2

1) In which location should I add the codepen html code?

If you're using a child theme, then for example, you could copy parent theme header.php file to your child theme directory and add the required html to that file after <head>.

If you're developing your own theme, then put the markup wherever you want on.

Or you could also add the markup with an action hook (e.g. wp_footer or theme specific action hook, if available). This could be added to your theme's functions.php file or a custom plugin file. Something along these lines,

add_action( 'wp_footer', 'prefix_my_preloader' ); // or other suitable template hook
function prefix_my_preloader() {
  ?>
    <div id="cooking">
      <!-- rest of the markup -->
    </div>
  <?php
}

2) In which location should I add given CSS?

Styles can be added either to your (child) theme's styles.css file, Customizer's Additional css section (if available, but maybe not the best option), or a custom css file that is located either in your (child) theme or custom plugin directory. Remeber to include the styles if you're using a custom css file.

3) And in which location should I add any required JS to make it work like it happens here

Scripts can be added either to your (child) theme's scripts.js file or a custom js file that is located either in your (child) theme or custom plugin directory. Remeber to include the scripts if you're using a custom js file.

P.S You can also add the styles and scripts with custom functions using action hooks as mentioned on the accepted answer for Add CSS animation as Preloader to WordPress. You would then add these functions either to your (child) theme's functions.php or a custom plugin file.

  • Adding something here, to achieve the expected behavior of preloader, you need to show it on page load and then hide/fadeout after page load completed. to do that you might want to use something like this. (this needs jquery), make sure your preloader have display:block in your css. this will override it when page load completes. <script> $(document).ready(function() { $(window).on("load", function() { preloaderFadeOutTime = 500; function hidePreloader() { var preloader = $('.spinner-wrapper'); preloader.fadeOut(preloaderFadeOutTime); } hidePreloader(); }); }); </script> – Vishwa Apr 29 at 5:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.