-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
49 lines (43 loc) · 1.32 KB
/
plugin.php
File metadata and controls
49 lines (43 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/**
* Plugin Name: Neobabis Views Counter
* Description: Basic code for Pages, Posts (or Custom posts types) views counter
* Author: Neokazis Charalampos
* Author URI: https://neobabis.gr
* Version: 1.0
*/
// Increase views counter by one
function neobabis_set_views_counter()
{
$key = 'views_counter';
$id = get_the_ID();
$counter = (int) get_post_meta($id, $key, true);
$counter++;
update_post_meta($id, $key, $counter);
}
// Returns number of views
function neobabis_get_views_counter()
{
$counter = get_post_meta(get_the_ID(), 'views_counter', true);
return "$counter views";
}
// For filter: manage_$_columns
function neobabis_views_counter_columns($columns)
{
$columns['views'] = 'Views';
return $columns;
}
// For action: manage_$_custom_column
function neobabis_views_counter_custom_column($column)
{
if ($column === 'views') {
echo neobabis_get_views_counter();
}
}
// WordPress Filters and actions
// Posts
add_filter('manage_posts_columns', 'neobabis_views_counter_columns');
add_action('manage_posts_custom_column', 'neobabis_views_counter_custom_column');
// Pages
add_filter('manage_pages_columns', 'neobabis_views_counter_columns');
add_action('manage_pages_custom_column', 'neobabis_views_counter_custom_column');