Last active
November 20, 2015 16:52
-
-
Save X-Raym/896dfa97101fc17c1250 to your computer and use it in GitHub Desktop.
Add Total Views column on Admin list posts pages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add Total Views column on Admin list posts pages | |
*/ | |
add_action( 'admin_init', 'hook_wpp_views_posts_column', 10 ); | |
function hook_wpp_views_posts_column() { | |
$post_types = get_post_types( array( 'public' => true ) ); | |
if ( empty( $post_types ) ) | |
return; | |
foreach ( $post_types as $post_type ) { | |
add_filter( "manage_edit-{$post_type}_columns", 'add_wpp_views_posts_column' ); // Old hooks: seems more compatible with certain custom post type plugin | |
//add_action( "manage_edit-{$post_type}_posts_custom_column", 'display_wpp_views_posts_column', 10, 2 ); // Old hooks | |
//add_filter( "manage_{$post_type}_posts_columns", 'add_wpp_views_posts_column' ); // New hooks | |
add_action( "manage_{$post_type}_posts_custom_column", 'display_wpp_views_posts_column', 10, 2 ); // New hooks | |
} | |
} | |
function add_wpp_views_posts_column($posts_columns) { | |
$posts_columns['wpp_views'] = __( 'Views', 'site-plugin'); | |
return $posts_columns; | |
} | |
function display_wpp_views_posts_column($column_name, $post_id) { | |
if ('wpp_views' == $column_name) { | |
echo wpp_get_views( $post_id ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment