Created
February 8, 2023 22:05
-
-
Save birgire/114ed2da8896d5f756a5668fc93c8b13 to your computer and use it in GitHub Desktop.
WordPress Plugin: My Jetpack Stats Column
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 | |
/** | |
* Plugin Name: My Jetpack Stats Column | |
* Plugin URI: http://xlino.com/projects/adding-a-stats-column-for-jetpack/ | |
* Author: birgire | |
* Description: Adds a stats column to the posts table on the edit.php screen | |
* Version: 1.0.0 | |
*/ | |
add_action( 'admin_init', function() | |
{ | |
// Check if Jetpack Stats module is active | |
if( ! class_exists( 'Jetpack' ) || ! Jetpack::is_module_active( 'stats' ) ) | |
return; | |
if( class_exists( 'MyJetPackStatsColumn' ) ) | |
{ | |
// Initialize | |
$o = new MyJetPackStatsColumn; | |
$o->init( [ 'postx', 'page' ] ); | |
} | |
} ); | |
class MyJetPackStatsColumn | |
{ | |
public function init( $cpts = [] ) | |
{ | |
foreach( (array) $cpts as $cpt ) | |
{ | |
$cpt = sanitize_key( $cpt ); | |
add_action( "manage_{$cpt}_posts_custom_column", [ $this, 'column' ], 10, 2 ); | |
add_filter( "manage_{$cpt}_posts_columns", [ $this, 'columns' ] ); | |
} | |
if( is_array( $cpts ) && ! empty( $cpts) ) | |
add_action( 'admin_print_styles-edit.php', [ $this, 'style' ] ); | |
} | |
public function columns( array $columns ) | |
{ | |
$columns['my_jetpack_stats'] = __( '' ); | |
return $columns; | |
} | |
public function column( $column, $post_id ) | |
{ | |
if( 'my_jetpack_stats' !== $column ) | |
return; | |
printf( | |
'<a href="%s%d" target="_blank" title="%s"><span class="dashicons dashicons-chart-bar"></span></a>', | |
esc_url( admin_url( 'admin.php?page=stats&view=post&post=' ) ), | |
(int) $post_id, | |
esc_attr( __( 'Stats' ) ) | |
); | |
} | |
public function style() | |
{ | |
?><style> .column-my_jetpack_stats { width: 30px; }</style><?php | |
} | |
} // end class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment