Whether you're already familiar with the MB Views extension from Meta Box or it's a new thing to your ears, I will tell you right now. In particular, I’ll use that feature in a real case to create a simple reading progress bar in WordPress posts.
Introduce about the MB Views
MB Views is a powerful tool that helps to create and customize templates. Even though it's all based on code, it is more optimal than using PHP. You can see that clearer in several tutorials that we made before on our channel. You can also see in those tutorials that it is usually used to get and display data from custom fields created with Meta Box. But that’s not all the things it can do.
You can also customize all templates in WordPress with MB Views, even for post types that don't have Meta Box fields.
This practice will make it clearer.
Video Version
The Reading Progress Bar
The simple reading progress bar will be shown at the top of the singular page of each blog post, like this:
This bar will work as an indicator, letting the visitors know how much they have read and how much is left after all. Then, it may help to improve the user experience.
Before Getting Started
Let’s find out which tools we need for this practice:
First, we need the Meta Box plugin to have a framework to use MB Views for creating templates. You can install it directly from wordpress.org.
Besides, we obviously need the MB Views extension to create a template for the progress bar without touching theme’s files. You can install it individually. In the case that you have the Meta Box AIO, this extension is already included there.
Display the Reading Progress Bar
Go to Meta Box > Views, and create a new template.
In the Template tab, I just add some div tags to specify the area to display the bar.
<div id="reading-progress-frame"> <div id="reading-progress-bar"></div> </div>
In there:
reading-progress-frame
: This ID is used for the whole area that covers the reading progress bar when it is full.reading-progress-bar
: This ID is used for the bar only.
You can name them whatever you want.
Next, go to the CSS tab of the view, I will use the CSS to display the progress bar.
#reading-progress { position: fixed; width: 100%; height: 5px; z-index: 99999; top: 0; left: 0; } #reading-progress-fill { height: 3px; width: 100%; } #reading-progress-fill { background-color: #ff0000; }
Let’s go through it in more detail:
#reading-progress { position: fixed; width: 100%; height: 5px; z-index: 99999; top: 0; left: 0; }
These lines are to set an area to cover the progress bar.
In particular, these lines of code in this image help determine the position of the bar at the top of the page.
The code below is to specify the height and width of the progress bar.
#reading-progress-fill { height: 3px; width: 100%; }
Rather, the width should be based on the real percentage of reading progress. But I set it full width now just for positioning it in the initial.
For the color of the bar, I set the red color. You should change it to any color that matches your theme.
background-color: #ff0000;
That’s all for the display.
Now, move to the Settings section of the view. To apply this template to the singular pages of the blog post, set the Type as Singular. Then, choose the post type.
Normally, you can choose an option for the position, but, in this case, the progress bar is at the top of the page, and I’ve specified its position in the CSS section before. So, the template’s setting does not affect the bar position.
Now, go to a post on frontend, you can see the progress bar that we created. It now is all red, no matter whether I scroll the mouse or not.
To make it work following the scroll, let’s move to the next step.
Make the Reading Progress Bar Run
We should add action to the progress bar using JavaScript to trigger when the readers scroll the page as well as identify how much they did read.
But first, we should change the default width of the bar to be zero. It should start from zero, since the readers do so.
Then, go to the JavaScript tab, add the following code.
const readingProgress = document.querySelector('#reading-progress-fill'); document.addEventListener('scroll', function (e) { let w = (document.body.scrollTop || document.documentElement.scrollTop) / (document.documentElement.scrollHeight - document.documentElement.clientHeight) * 100; readingProgress.style.setProperty('width', w + '%'); });
In there:
const readingProgress = document.querySelector('#reading-progress-fill');
This is to create a JavaScript object for the progress bar. #reading-progress-fill
is the ID where we set the bar.
document.addEventListener('scroll', function (e) {:
This is to trigger the event that readers scroll the page.
I set the w
variable to calculate the percentage of the page that the reader scrolls. You should rename it.
The following formula will do that.
(document.body.scrollTop || document.documentElement.scrollTop) / (document.documentElement.scrollHeight - document.documentElement.clientHeight) * 100:
document.body.scrollTop || document.documentElement.scrollTop
: the number of the distance in pixels from the top to the place where the reader is.document.documentElement.scrollHeight
: the total height of the page where the readers can scroll, also in pixels.- document.documentElement.clientHeight
: to eliminate the padding on the page.
All these elements in the formula are the HTML DOM elements. You can refer to this link to learn more about them.
Finally, the result will be converted to percentage.
This line below will force the width of the progress bar to change following the result returned by the w
variable.
readingProgress.style.setProperty('width', w + '%');
That’s all.
After saving the template, go back to the page on frontend. Now, when we scroll down, the progress bar will appear, and gradually lengthen according to the amount of content read.
So we’ve already done it.
If you want to change the speed of change in the bar and make it more smoothly, you can add the code below:
transition: width 300ms ease;
Just change the number to get the effect that you expect.
Last Words
Hopefully, this tutorial will give you a hand in creating a simple reading bar by using the MB Views extension from Meta Box. You can see other tutorials to learn more about how MB Views is effective in getting and displaying data from Meta Box custom fields. Thanks for reading!
Other case studies you might be interested in
- Create A Dynamic Landing Page in WordPress Using Custom Field
- Create a Filter to Find Hotels by Location
- Create an OTA Website Like Booking.com with Meta Box Plugin - P1: Create a Page to Introduce Hotel Rooms
- Create an OTA Website Like Booking.com with Meta Box Plugin - P2: Create Filters on the Archive Page
- Create an OTA Website Like Booking.com with Meta Box Plugin - P3: Create Filters for Single Hotel Pages
- Create Dynamic Favicon in WordPress using Meta Box plugin
- Create Posts Series in WordPress Using Meta Box
- Display The Latest Products Section - P2 - Using Meta Box and Elementor
- Display The Latest Products Section - P3 - Using Meta Box And Oxygen
- How to Add Custom Fields for WooCommerce - P2 - Using MB Views
- How to Add Custom Fields to Display Banners using Meta Box Plugin
- How to Add Custom Fields to Products in WooCommerce - Using Meta Box and Bricks
- How to Add Guest Authors and Guest Posts - Using Meta Box
- How to Add Related Posts - Using Custom Fields
- How to Build a Hotel Booking Website Using Meta Box - P1
- How to Build a Hotel Booking Website Using Meta Box - P2 - Booking Page in Backend
- How to Build a Hotel Booking Website Using Meta Box - P4 - Booking Management Page
- How to Build a Hotel Booking Website Using Meta Box – P3 – Booking Page for Customer
- How to Create a Classified Ads Website using Meta Box
- How to Create a Custom 404 Page in WordPress - P1 - Using Meta Box and Elementor
- How to create a FAQs page - P5 - Using Meta Box and Breakdance
- How to Create a Product Page - P2 - Using Meta Box and Oxygen
- How to Create a Product Page - P3 - Using Meta Box and Bricks
- How to Create a Product Page - P4 - Using Meta Box and Elementor
- How to Create a Product Page - P5 - Using Meta Box and Gutenberg
- How to Create a Product Page - P6 -Using Meta Box and Breakdance
- How to Create a Product Page - P7 - Using Meta Box + Kadence
- How to Create a Product Page - P8 - Using Meta Box and Brizy
- How to Create a Product Page - P9 - Using Meta Box and Divi
- How to Create a Product Page using Meta Box Plugin
- How to Create a Reading Progress Bar in WordPress Posts - P1 - Using MB Views
- How to Create a Recipe - P2 - Using Meta Box and Oxygen
- How to Create a Recipe - P3 - Using Meta Box and Elementor
- How to Create a Recipe - P4 - Using Meta Box and Bricks
- How to Create a Recipe - P5 - Using Meta Box and Zion
- How to Create a Recipe - P6 - Using Meta Box and Brizy
- How to Create a Recipe - P7 - Using Meta Box and Breakdance
- How to Create a Recipe - P8 - Using Meta Box and Kadence
- How to Create a Recipe - P9 - Using Meta Box and Divi
- How to Create a Recipe with Meta Box Plugin
- How to Create a Simple Listing - P2 - Using Meta Box and Bricks
- How to Create a Simple Listing - P3 - Using Meta Box and Breakdance
- How to Create a Simple Listing - P4 - Using Meta Box and Elementor
- How to Create a Team Members Page - P1- Using Meta Box and Elementor
- How to Create a Team Members Page - P2 - Using Meta Box and Oxygen
- How to Create a Team Members Page - P3 - Using Meta Box and Bricks
- How to Create a Team Members Page - P4 - Just Meta Box
- How to Create a Team Members Page - P6 - using Meta Box and Breakdance
- How to Create a Team Members Page - P7 - Using Meta Box and Kadence
- How to Create a Video Gallery Page - P2 - Using Meta Box + Bricks
- How to Create a Video Gallery Page - P3 - Using Meta Box and Breakdance
- How to Create a Video Gallery Page - P4 - Using Meta Box + Elementor
- How to Create a Video Gallery Page - P5 - Using MB Views
- How to Create a Video Gallery Page - P6 - Using Meta Box and Zion
- How to Create a Video Gallery Page Using Meta Box + Oxygen
- How to Create ACF Flexible Content Field with Meta Box
- How to Create an Auto-Updated Cheat Sheet in WordPress
- How to Create an FAQs Page - P1 - Using Meta Box and Elementor
- How to create an FAQs page - P2 - Using Meta Box and Oxygen
- How to create an FAQs page - P4 - Using Meta Box and Bricks
- How to Create an FAQs Page - P6 - Using MB Views
- How to Create an FAQs Page - P7 - Using Meta Box and Divi
- How to Create an FAQs Page - P8 - Using Meta Box and Kadence
- How to Create an FAQs Page - P9 - Using MB Blocks
- How to Create an FAQs Page -P3- Using Meta Box
- How to Create Buttons with Dynamic Link using Custom Fields
- How to Create Category Thumbnails & Featured Images Using Custom Fields
- How to Create Download and Preview Buttons - P1 - Using Meta Box and Bricks
- How to Create Download and Preview Buttons - P2 - Using Meta Box and Oxygen
- How to Create Download and Preview Buttons - P3 - Using MB Views
- How to Create Download Buttons in WordPress - Using Custom Fields
- How to Create Dynamic Landing Page in WordPress - P1 - Using Meta Box and Elementor
- How to Create Dynamic Landing Page in WordPress - P2 - Using Meta Box and Bricks
- How to Create Menus for Restaurants - P1 - Using Meta Box and Elementor
- How to Create Menus for Restaurants - P2- Using Meta Box and Bricks
- How to Create Notification Using Custom HTML Field
- How to Create Online Admission Form for School or University
- How to Create Online Reservation Form for Restaurants using Meta Box
- How to Create Relationships - P1 - Using Meta Box and Oxygen
- How to Create Relationships - P2 - Using Meta Box and Bricks
- How to Create Relationships - P3 - Using MB Views
- How to Create Relationships - P4 - Using Meta Box and Breakdance
- How to Create Taxonomy Thumbnails & Featured Images - P2 - Using Meta Box and Oxygen
- How to Create Taxonomy Thumbnails & Featured Images - P3 - Using Meta Box and Bricks
- How to Create Taxonomy Thumbnails & Featured Images - P4 - Using MB Views
- How to Create YouTube Video Timestamps on WordPress Website - P1 - Using MB Views
- How to Display a Video Playlist - P1- Using MB Views
- How To Display All Listings On A Map With Meta Box
- How to Display Author Bio in WordPress - P1 - Using Meta Box and Bricks
- How to Display Author Bio in WordPress - P2 - Using MB Views
- How to Display Dynamic Banners in WordPress - P3 - Using MB Views
- How to Display Images from Cloneable Fields - P1 - with Gutenberg
- How to Display Images from Cloneable Fields - P2 - Using Meta Box and Oxygen
- How to Display Images from Cloneable Fields - P3 - with Elementor
- How to Display Images from Cloneable Fields - P4 - with Bricks
- How to Display Opening Hours for Restaurants - P1 - Using Meta Box + Gutenberg
- How to Display Opening Hours for Restaurants - P2 - Using Meta Box and Oxygen
- How to Display Product Variations - P1 - Using Meta Box and Gutenberg
- How to Display Product Variations - P2 - Using Meta Box and Oxygen