/
posts-n-pages.php
100 lines (64 loc) · 2.05 KB
/
posts-n-pages.php
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/*
Plugin Name: Post 'n Page Views
Plugin URI: http://www.designbyrono.com/posts-n-pages/
Description: Tracks the number of views in pages and posts, in the admin area. To show view counts in your post/page template, add this snippet of code: <code><?php print_page_views(get_the_ID()); ?></code>. Requires <em>JetPack by Wordpress.com</em>.
Version: 1.0
Author: Rono
Author URI: http://www.designbyrono.com
*/
// ----- initialize
add_action('admin_head', 'add_views_style');
add_action('in_admin_header', 'wp_get_post_stats');
add_action('wp_head', 'wp_get_post_stats');
add_action('manage_posts_custom_column', 'show_views_in_row',10,2);
add_action('manage_pages_custom_column', 'show_views_in_row',10,2);
add_filter('manage_pages_columns', 'show_views_in_header');
add_filter('manage_posts_columns', 'show_views_in_header');
// ----- add_views_style()
function add_views_style(){
echo
'<style type="text/css">
.column-views {
width: 60px;
text-align: right !important;
}
</style>';
}
// ----- show_views_in_header()
function show_views_in_header($columns){
$columns['views'] = __('Views');
return $columns;
}
// ----- show_views_row()
function show_views_in_row($column_name, $post_id){
if ($column_name != 'views') return;
$pv = wp_get_post_views($post_id);
echo $pv ? $pv : 0;
}
// ----- main()
if (!function_exists('print_page_views')):
function print_page_views($post_id) {
$views = wp_get_post_views($post_id);
$views = $views ? $views : 0;
echo number_format_i18n($views) . ' views';
}
endif;
// ----- wp_get_post_stats()
function wp_get_post_stats() {
$post_stats = null;
if (function_exists('stats_get_csv')) {
global $post_stats;
$post_stats = stats_get_csv('postviews', '&days=-1&limit=-1&summarize');
}
}
// ----- wp_get_post_views()
function wp_get_post_views($post_id) {
global $post_stats;
foreach ($post_stats as $p) {
if ($p['post_id'] == $post_id) {
return $p['views'];
}
}
}
?>