Count Views For Manga Chapters

MrSam_1

Well-known member
Administrative
Trusted Seller
Dec 1, 2018
22,099
25,884
120
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.
 
  • Like
Reactions: trixonv

MrSam_1

Well-known member
Administrative
Trusted Seller
Dec 1, 2018
22,099
25,884
120
Use your reading skillz. Another thing you would find out if you kept reading is that the code is also split for different versions. Thing you forgot to mention in your request: what version of madara are you using?
 

trixonv

New member
Sep 23, 2020
14
0
1
Use your reading skillz. Another thing you would find out if you kept reading is that the code is also split for different versions. Thing you forgot to mention in your request: what version of madara are you using?
i use 1.6.5
 

MrSam_1

Well-known member
Administrative
Trusted Seller
Dec 1, 2018
22,099
25,884
120
You have to activate child theme after you paste the function.

Also this is the full code that have to be added to functions.php in your child theme

Code:
/**
* 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;
}


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


}

As I said, if you have kept reading there is this paragraph: Full code for Madara-Core 1.6+ :
 
  • Love
Reactions: trixonv

trixonv

New member
Sep 23, 2020
14
0
1
thnx alot its working

but i use arabic that mean from right to left if you can tell me how to edit
 

Attachments

  • qzsedzezeqzeqz.png
    qzsedzezeqzeqz.png
    38.7 KB · Views: 67

MrSam_1

Well-known member
Administrative
Trusted Seller
Dec 1, 2018
22,099
25,884
120
I don't know how to help you set RTL on this as I don't know madara theme hooks and also don't have time to check it.

I suppose is this trigger:

add_action('wp_manga_after_chapter_name', 'my_manga_chapter_name_info', 10, 2);

but instead of "after_chapter_name" is "before_chapter_name"
 
  • Like
Reactions: testeradm

magic597

New member
Jun 9, 2022
6
1
3
Florida
can you help me too?
Version 1.7.3.1
You have to activate child theme after you paste the function.

Also this is the full code that have to be added to functions.php in your child theme

Code:
/**
* 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;
}


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


}

As I said, if you have kept reading there is this paragraph: Full code for Madara-Core 1.6+ :
Add Plugin > Search for Code Snippets > Install > Activate > Copy the code above > Paste it in a PHP tab > Give it a title > Activate.

You'd probably want to also have the child theme of Madara installed and activated to avoid any pesky bugs.
 

magic597

New member
Jun 9, 2022
6
1
3
Florida
my sql table name is njgxm2_
Can you edit the code properly for me? please
PHP:
/**
* Add columns "views" to table njgxm2_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 . 'njgxm2_manga_chapters', 'views')){
$db->alter_add_column($wpdb->prefix . 'njgxm2_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}njgxm2_manga_chapters WHERE chapter_id=%d" ;


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


return $views ? $views : 0;
}


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($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}njgxm2_manga_chapters", array('views'=>++$views), array('chapter_id' => $chapter_id));
}
}


}
 

lazyboyyy

New member
Oct 11, 2022
5
0
1
PHP:
/**
* Add columns "views" to table njgxm2_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 . 'njgxm2_manga_chapters', 'views')){
$db->alter_add_column($wpdb->prefix . 'njgxm2_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}njgxm2_manga_chapters WHERE chapter_id=%d" ;


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


return $views ? $views : 0;
}


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($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}njgxm2_manga_chapters", array('views'=>++$views), array('chapter_id' => $chapter_id));
}
}


}
1673406426775.png
what's wrong?
 

Latest posts

Forum statistics

Threads
68,452
Messages
903,930
Members
228,863
Latest member
mahdigolab

About us

  • Our community has been around for many years and pride ourselves on offering unbiased, critical discussion among people of all different backgrounds. We are working every day to make sure our community is one of the best.

Quick Navigation

User Menu