2018-11-23_153728

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 Views. Work for Madara Core prior to 1.6
**/
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));
}
}
}

If you are using Madara-Core from 1.6+, change the “Increase Views” function by this one (this one will support counting views if you use Page Cache)

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 = '';
if(isset($_POST['chapter']) && $_POST['chapter'] != 'undefined'){
$chapter_slug = $_POST['chapter'];
}

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

}

Full code for Madara-Core 1.6+ : Source Code

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 🙂

SHARE THIS POST

3 Comments

Leave a Reply

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

*

four + 1 =