Add a Loading Screen to Genesis
You’ve likely seen them before…when a page is loading, a little icon spins and disappears when the page is done loading. Page loading images can be a nice user experience tool because it lets your viewers know for sure that the page is loading. If you display a lot of media that causes slower page loads, a user might think the page has frozen if all he/she has to look at is a blank screen. Adding a loading image can convey that the page is working and has not frozen. After some research, there are a few plugins available but I decided to type up a quick tutorial on how to create a custom page loading screen in the Genesis Framework. Follow along!
Add the Action to genesis_before
There are probably different hooks you can use to insert the following function but I used the genesis_before action.
add_action( 'genesis_before', 'rv_add_preloader', 1 ); function rv_add_preloader() { echo '<div class="preloader"></div>'; }
Add the jQuery
This jQuery script goes into a js file that you can name something like page-loader-init.js.
jQuery(window).load(function() { jQuery(".preloader").fadeOut("slow"); })
Save this file to your child-theme’s /js directory. From there, you will need to enqueue it using:
add_action( 'wp_enqueue_scripts', 'rv_enqueue_preloader_script' ); function rv_enqueue_preloader_script() { wp_enqueue_script( 'page-loader-init', get_stylesheet_directory_uri() . '/js/page-loader-init.js', array( 'jquery' ), CHILD_THEME_VERSION ); }
Add some CSS
Add this CSS to your style.css:
.preloader { position: fixed; left: 0px; top: 0px; width: 100%; height: 100%; z-index: 9999; background: url('[path-to-gif-file]') 50% 50% no-repeat rgb(255,255,255); }
That’s it! If you are a designer and wish to design your own loading gif, awesome. Otherwise, check out Preloaders.net to create and download a free gif.
Suzie says
This may be a really basic question but where do the enqueue lines go?
Ren says
It can go in your functions.php file, the main plugin file if you’re using a plugin, or any other php file that is run on every page.
Suzie says
Unfortunately, I can’t get it to work. All the code is done correctly :/
Ren says
I updated the post so try running through everything again.
Suzie says
I noticed… Just redid everything again however it still isn’t working.
Ren says
It’s hard for me to tell what may be going on without seeing how everything looks. If you’d like me to take a look and get this working for you, you can get in touch here.
Rob says
Im trying to figure out the preloader for wordpress where the number shows up all the way to 100% like on the website above
Avinash D'Souza says
Hey Ren,
How about a loading bar across the top of the screen like any of the posts in Medium.com?
This is a ready script but I have no idea of how to use it…http://ricostacruz.com/nprogress/
Avinash D'Souza says
Actually, this is a lot more robust…http://github.hubspot.com/pace/
And it’s Hubspot supported so…
Ren says
Hey, Avinash. Either of those can be integrated to work with Genesis. If you need help getting one to work with your site, drop me a message.
DJ says
Very cool tutorial, Ren. I also read your post on Headway vs. Genesis and it was very helpful. I decided to go with Headway because I’m not a coder and I’m afraid of the thought of messing with anything other than a little html. Would you know how to add a page loading screen in Headway? Thanks!
Ren says
Hi DJ. Thanks for commenting. I have not tried to add a loading screen to Headway but I’ll do a little bit of digging and ask around. If I come up with anything I will let you know. If not, I will add it to the list of future tutorials. 🙂
Ren says
Hey DJ. Just following up on your comment from before. Corey Freeman from Headway101 was kind enough to type up a tutorial explaining how to add a loading screen in Headway. You can check it out at http://engagewp.com/add-page-loading-screen-headway-themes/.