-
Notifications
You must be signed in to change notification settings - Fork 0
/
jetpack-views-column.php
45 lines (35 loc) · 1.24 KB
/
jetpack-views-column.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
<?php
// Add a Views columns to insert jetpack postviews data into
function rdc_add_views_column( $cols ) {
$cols['pageviews'] = 'Views';
return $cols;
}
add_filter( 'manage_edit-post_columns', 'rdc_add_views_column' );
// Grab and display the postviews data from Jetpack for each post
function rdc_add_views_colurdc_data( $colname ) {
global $post;
// Make sure we're inserting into the correct column
if ( 'pageviews' !== $colname )
return false;
// Make sure jetpack and stats are available
if ( ! ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'stats' ) ) ) {
echo 'Error 101';
return false;
}
// Make sure stats_get_csv is available
if ( ! function_exists( 'stats_get_csv' ) ) {
echo 'Error 102';
return false;
}
// Get the post data from Jetpack
$postviews = stats_get_csv( 'postviews', "post_id={$post->ID}" );
// We have a problem if there was no data returned
if ( ! $postviews ) {
echo 'Error 103';
return false;
}
// Print Jetpack post views
echo '<strong>' . number_format( $postviews[0]['views'] ) . '</strong>';
}
add_action( 'manage_posts_custom_column', 'rdc_add_views_colurdc_data' );
// omit