Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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