2018-11-23_153728

Many users have asked whether it’s possible to count views for manga chapters in WordPress, especially for those using the Madara theme. While we plan to develop a plugin for this feature, it takes time. So, I’ve created this simple guide to help you implement chapter view counting in just a few minutes!

Even if you don’t have coding experience, you can simply copy and paste the provided code into your child theme’s functions.php file.

1. Quick Setup for Counting Views in Manga Chapters

If you want a quick solution, here’s what you need to do:

1️⃣ Copy the full source code from this link.
2️⃣ Paste the code into your child theme’s functions.php file.
3️⃣ Switch to the parent theme, then switch back to the child theme to activate the code.

This method will add a view counter for each manga chapter in your Madara-based site.

2. Full Code to Count Views for Manga Chapters

A. Adding a “Views” Column to the Manga Chapters Table

First, we need to add a new column to store chapter views in the database. This function runs when switching themes:

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

B. Displaying Views on Manga Chapters

Next, we display the view count on each manga chapter page:

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

C. Increasing View Count (Madara Core 1.5 and Below)

For Madara Core versions prior to 1.6, use the following function to increase views when a chapter is loaded:

php
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);

if($chapter){
$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));
}
}
}

D. Counting Views for Madara Core 1.6+ (Supports Page Cache)

For Madara Core 1.6+, replace the function above with this one to ensure compatibility with page caching:

php
add_action('wp_manga_after_update_manga_views', 'my_wp_manga_after_update_manga_views');

function my_wp_manga_after_update_manga_views(){
$manga_id = intval($_POST['manga']);
$chapter_slug = isset($_POST['chapter']) && $_POST['chapter'] != 'undefined' ? $_POST['chapter'] : '';

if($chapter_slug != ''){
global $wp_manga_chapter;
$chapter = $wp_manga_chapter->get_chapter_by_slug($manga_id, $chapter_slug);

if($chapter){
$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));
}
}
}

3. How This Works

Here’s a breakdown of the process:

Step 1: Add a views column to the manga_chapters table.
Step 2: Use after_manga_single to increase the view count each time a chapter is loaded.
Step 3: Display the number of views using wp_manga_after_chapter_name.

This method ensures that each manga chapter gets a proper view counter, making it easier to track reader engagement.

4. Conclusion: Start Counting Views for Manga Chapters Today!

With this simple method, you can easily count views for manga chapters in WordPress using the Madara theme. Whether you’re using Madara Core 1.5 or 1.6+, you now have the right solution!

💡 Try it out and let us know if it works for you! 🚀

SHARE THIS POST

3 Comments

Leave a Reply

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

*

thirteen + 12 =