As few customers ask, is it possible to count views for chapter? Well, we intend to develop a plugin for that, but it is time consuming so I wrote a guide on this blog first. It just take few minutes to understand and implement the code.

If you don’t understand about coding, this is the full code for lazy ones. Just copy the code and paste it into your child theme /functions.php file. After that, switch to parent theme and the switch back to child theme to activate the code.

(Please copy code from here: https://codeshare.io/GbYY1j)

* Add columns “views” to table manga_chapters
add_action(“after_switch_theme”, “my_manga_add_columns”);
function my_manga_add_columns(){
global $wpdb;

$db = WP_MANGA_DATABASE::get_instance();
if(!$db->column_exists($wpdb->prefix . ‘manga_chapters’, ‘views’)){
$db->alter_add_column($wpdb->prefix . ‘manga_chapters’, ‘views’, ‘int’);

* Show the views
add_action(‘wp_manga_after_chapter_name’, ‘my_manga_chapter_name_info’, 10, 2);

function my_manga_chapter_name_info($chapter, $manga_id){
$views = manga_get_chapter_views($chapter[‘chapter_id’]);

echo ‘<span class=”view”><i class=”fa fa-eye”></i> ‘ . $views . ‘</span>’;

function manga_get_chapter_views($chapter_id){
global $wpdb;
$sql = “SELECT views from {$wpdb->prefix}manga_chapters WHERE chapter_id=%d” ;

$views = $wpdb->get_var($wpdb->prepare($sql, $chapter_id));

return $views ? $views : 0;

* Increase the views
add_action(‘after_manga_single’, ‘my_manga_chapter_views’);
function my_manga_chapter_views(){
$cur_chap = get_query_var( ‘chapter’ );
$manga_id = get_the_ID();

if($cur_chap != ”){
global $wp_manga_chapter;

$chapter = $wp_manga_chapter->get_chapter_by_slug( $manga_id, $cur_chap );

$chapter_id = $chapter[‘chapter_id’];
global $wpdb;
$views = manga_get_chapter_views($chapter_id);

$wpdb->update(“{$wpdb->prefix}manga_chapters”, array(‘views’=>++$views), array(‘chapter_id’ => $chapter_id));

Now I will break down the code for you to understand. To count views, first we need to add new columns to manga_chapters table to store the value. We use “after_switch_theme” action to call the function “my_manga_add_columns” to alter table and add new column.

Then, for each loading of chapter, we consider 1 view. We use action “after_manga_single” to hook and increase the views for that chapter.

Then, to display the views, we use action “my_manga_chapter_name_info” to get the views from database and echo to screen.

That’s it.

Tell us the result if you are happy with it 🙂


Leave a Reply

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


12 − 9 =