Skip to content

Instantly share code, notes, and snippets.

@artikus11
Created August 11, 2021 16:38
Show Gist options
  • Save artikus11/ec7cdb85a8a5585627d62f3742bf2ddb to your computer and use it in GitHub Desktop.
Save artikus11/ec7cdb85a8a5585627d62f3742bf2ddb to your computer and use it in GitHub Desktop.
Добавление новой колонки с ID постов
/**
* Добаление новой колонки
*
* @param $columns
*
* @return string[]
*
* @author Artem Abramovich
* @verphp 7.0
*/
function art_add_column_posts_id( $columns ): array {
$new_column = [ 'id' => 'ID' ];
return array_slice( $columns, 0, 1, true ) + $new_column + array_slice( $columns, 1, null, true );
}
add_filter( 'manage_pages_columns', 'art_add_column_posts_id', 5 );
add_filter( 'manage_posts_columns', 'art_add_column_posts_id', 5 );
/**
* Заполнение колонки данными
*
* @param string $column_name Идентификатор колонки.
* @param int $id ID записи.
*
* @author Artem Abramovich
* @verphp 7.0
*/
function art_fill_columns_post_id( string $column_name, int $id ) {
if ( $column_name === 'id' ) {
echo $id;
}
}
add_action( 'manage_posts_custom_column', 'art_fill_columns_post_id', 5, 2 );
add_action( 'manage_pages_custom_column', 'art_fill_columns_post_id', 5, 2 );
/**
* Добавляем стили для колонки
*
* @author Artem Abramovich
* @verphp 7.0
*/
function art_add_column_css() {
echo '<style>.column-id{ width:50px; }</style>';
}
add_action( 'admin_head', 'art_add_column_css' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment