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:

A simple reading progress bar is at the top of the singular page of each blog post

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.

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 the Template tab, just add some div tags to specify the area to display the bar.

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;
}

Add code to the CSS tab

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.

Some code to 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.

Set the bar full width 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.

In the Settings section, set location to apply this template to the singular pages of the blog post

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.

Set the position for the template, it 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.

Before using Javascript, the progress bar is all red no matter if 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.

Change the default width of the bar to be zero

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 + '%');
});

Add code to the JavaScript tab

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.

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.

The reading progress bar run after using Javascript

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;

Add code to change the speed of change in the bar and make it more smoothly

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

  1. Create A Dynamic Landing Page in WordPress Using Custom Field
  2. Create a Filter to Find Hotels by Location
  3. Create an OTA Website Like Booking.com with Meta Box Plugin - P1: Create a Page to Introduce Hotel Rooms
  4. Create an OTA Website Like Booking.com with Meta Box Plugin - P2: Create Filters on the Archive Page
  5. Create an OTA Website Like Booking.com with Meta Box Plugin - P3: Create Filters for Single Hotel Pages
  6. Create Dynamic Favicon in WordPress using Meta Box plugin
  7. Create Posts Series in WordPress Using Meta Box
  8. Display The Latest Products Section - P2 - Using Meta Box and Elementor
  9. Display The Latest Products Section - P3 - Using Meta Box And Oxygen
  10. How to Add Custom Fields for WooCommerce - P2 - Using MB Views
  11. How to Add Custom Fields to Display Banners using Meta Box Plugin
  12. How to Add Custom Fields to Products in WooCommerce - Using Meta Box and Bricks
  13. How to Add Guest Authors and Guest Posts - Using Meta Box
  14. How to Add Related Posts - Using Custom Fields
  15. How to Build a Hotel Booking Website Using Meta Box - P1
  16. How to Build a Hotel Booking Website Using Meta Box - P2 - Booking Page in Backend
  17. How to Build a Hotel Booking Website Using Meta Box - P4 - Booking Management Page
  18. How to Build a Hotel Booking Website Using Meta Box – P3 – Booking Page for Customer
  19. How to Create a Classified Ads Website using Meta Box
  20. How to Create a Custom 404 Page in WordPress - P1 - Using Meta Box and Elementor
  21. How to create a FAQs page - P5 - Using Meta Box and Breakdance
  22. How to Create a Product Page - P2 - Using Meta Box and Oxygen
  23. How to Create a Product Page - P3 - Using Meta Box and Bricks
  24. How to Create a Product Page - P4 - Using Meta Box and Elementor
  25. How to Create a Product Page - P5 - Using Meta Box and Gutenberg
  26. How to Create a Product Page - P6 -Using Meta Box and Breakdance
  27. How to Create a Product Page - P7 - Using Meta Box + Kadence
  28. How to Create a Product Page - P8 - Using Meta Box and Brizy
  29. How to Create a Product Page - P9 - Using Meta Box and Divi
  30. How to Create a Product Page using Meta Box Plugin
  31. How to Create a Reading Progress Bar in WordPress Posts - P1 - Using MB Views
  32. How to Create a Recipe - P2 - Using Meta Box and Oxygen
  33. How to Create a Recipe - P3 - Using Meta Box and Elementor
  34. How to Create a Recipe - P4 - Using Meta Box and Bricks
  35. How to Create a Recipe - P5 - Using Meta Box and Zion
  36. How to Create a Recipe - P6 - Using Meta Box and Brizy
  37. How to Create a Recipe - P7 - Using Meta Box and Breakdance
  38. How to Create a Recipe - P8 - Using Meta Box and Kadence
  39. How to Create a Recipe - P9 - Using Meta Box and Divi
  40. How to Create a Recipe with Meta Box Plugin
  41. How to Create a Simple Listing - P2 - Using Meta Box and Bricks
  42. How to Create a Simple Listing - P3 - Using Meta Box and Breakdance
  43. How to Create a Simple Listing - P4 - Using Meta Box and Elementor
  44. How to Create a Team Members Page - P1- Using Meta Box and Elementor
  45. How to Create a Team Members Page - P2 - Using Meta Box and Oxygen
  46. How to Create a Team Members Page - P3 - Using Meta Box and Bricks
  47. How to Create a Team Members Page - P4 - Just Meta Box
  48. How to Create a Team Members Page - P6 - using Meta Box and Breakdance
  49. How to Create a Team Members Page - P7 - Using Meta Box and Kadence
  50. How to Create a Video Gallery Page - P2 - Using Meta Box + Bricks
  51. How to Create a Video Gallery Page - P3 - Using Meta Box and Breakdance
  52. How to Create a Video Gallery Page - P4 - Using Meta Box + Elementor
  53. How to Create a Video Gallery Page - P5 - Using MB Views
  54. How to Create a Video Gallery Page - P6 - Using Meta Box and Zion
  55. How to Create a Video Gallery Page Using Meta Box + Oxygen
  56. How to Create ACF Flexible Content Field with Meta Box
  57. How to Create an Auto-Updated Cheat Sheet in WordPress
  58. How to Create an FAQs Page - P1 - Using Meta Box and Elementor
  59. How to create an FAQs page - P2 - Using Meta Box and Oxygen
  60. How to create an FAQs page - P4 - Using Meta Box and Bricks
  61. How to Create an FAQs Page - P6 - Using MB Views
  62. How to Create an FAQs Page - P7 - Using Meta Box and Divi
  63. How to Create an FAQs Page - P8 - Using Meta Box and Kadence
  64. How to Create an FAQs Page - P9 - Using MB Blocks
  65. How to Create an FAQs Page -P3- Using Meta Box
  66. How to Create Buttons with Dynamic Link using Custom Fields
  67. How to Create Category Thumbnails & Featured Images Using Custom Fields
  68. How to Create Download and Preview Buttons - P1 - Using Meta Box and Bricks
  69. How to Create Download and Preview Buttons - P2 - Using Meta Box and Oxygen
  70. How to Create Download and Preview Buttons - P3 - Using MB Views
  71. How to Create Download Buttons in WordPress - Using Custom Fields
  72. How to Create Dynamic Landing Page in WordPress - P1 - Using Meta Box and Elementor
  73. How to Create Dynamic Landing Page in WordPress - P2 - Using Meta Box and Bricks
  74. How to Create Menus for Restaurants - P1 - Using Meta Box and Elementor
  75. How to Create Menus for Restaurants - P2- Using Meta Box and Bricks
  76. How to Create Notification Using Custom HTML Field
  77. How to Create Online Admission Form for School or University
  78. How to Create Online Reservation Form for Restaurants using Meta Box
  79. How to Create Relationships - P1 - Using Meta Box and Oxygen
  80. How to Create Relationships - P2 - Using Meta Box and Bricks
  81. How to Create Relationships - P3 - Using MB Views
  82. How to Create Relationships - P4 - Using Meta Box and Breakdance
  83. How to Create Taxonomy Thumbnails & Featured Images - P2 - Using Meta Box and Oxygen
  84. How to Create Taxonomy Thumbnails & Featured Images - P3 - Using Meta Box and Bricks
  85. How to Create Taxonomy Thumbnails & Featured Images - P4 - Using MB Views
  86. How to Create YouTube Video Timestamps on WordPress Website - P1 - Using MB Views
  87. How to Display a Video Playlist - P1- Using MB Views
  88. How To Display All Listings On A Map With Meta Box
  89. How to Display Author Bio in WordPress - P1 - Using Meta Box and Bricks
  90. How to Display Author Bio in WordPress - P2 - Using MB Views
  91. How to Display Dynamic Banners in WordPress - P3 - Using MB Views
  92. How to Display Images from Cloneable Fields - P1 - with Gutenberg
  93. How to Display Images from Cloneable Fields - P2 - Using Meta Box and Oxygen
  94. How to Display Images from Cloneable Fields - P3 - with Elementor
  95. How to Display Images from Cloneable Fields - P4 - with Bricks
  96. How to Display Opening Hours for Restaurants - P1 - Using Meta Box + Gutenberg
  97. How to Display Opening Hours for Restaurants - P2 - Using Meta Box and Oxygen
  98. How to Display Product Variations - P1 - Using Meta Box and Gutenberg
  99. How to Display Product Variations - P2 - Using Meta Box and Oxygen

Leave a Reply

Your email address will not be published. Required fields are marked *