Skip to content

Instantly share code, notes, and snippets.

@mattyrob
Created October 22, 2020 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattyrob/89fceba409232e8874e38f3855bb18e8 to your computer and use it in GitHub Desktop.
Save mattyrob/89fceba409232e8874e38f3855bb18e8 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Add Post / Page ID Column
Description: Add ID column on Post and Page listing in the ClassicPress admin area.
Author: Matt Robinson
Version: 0.1
*/
if ( is_admin() ) {
$add_admin_id = new Add_Admin_Id();
}
class Add_Admin_Id {
public function __construct() {
add_action( 'load-edit.php', array( $this, 'add_id_init' ) );
}
public function add_id_init() {
$screen = get_current_screen();
if ( ! isset( $screen->post_type ) || ! in_array( $screen->post_type, array( 'post', 'page' ), true ) ) {
return;
}
add_filter( "manage_{$screen->id}_columns", array( $this, 'add_id_col' ) );
add_action( 'admin_head', array( $this, 'add_id_style' ) );
add_action( "manage_{$screen->post_type}_posts_custom_column", array( $this, 'add_id_data_cb' ), 10, 2 );
add_filter( "manage_{$screen->id}_sortable_columns", array( $this, 'add_id_data_sortable' ) );
}
public function add_id_col( $cols ) {
$cols = array_reverse( $cols, true );
$cb = array_pop( $cols );
$cols['id'] = 'ID';
$cols['cb'] = 'cb';
return array_reverse( $cols, true );
}
public function add_id_style() {
echo '<style>.wp-list-table .column-id { width: 5%; }</style>';
}
public function add_id_data_cb( $col, $post_id ) {
if ( 'id' === $col ) {
echo esc_html( $post_id );
}
}
public function add_id_data_sortable( $cols ) {
$cols['id'] = 'template';
return $cols;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment