This modification will provide you with a more efficient way of organizing and keeping track of your blocks. With the added Category column, you can easily categorize and sort your blocks based on their specific use or purpose. The Screenshot column will also make it easier to identify blocks at a glance, as you can quickly see a preview of what they look like.
<?php
// Add custom column to post table
add_filter( 'manage_oxy_user_library_posts_columns', 'add_custom_column_to_post_table' );
function add_custom_column_to_post_table( $columns ) {
$columns['_ct_connection_page_category'] = __( 'Category', 'text-domain' );
return $columns;
}
// Display custom field data in the custom column
add_action( 'manage_oxy_user_library_posts_custom_column', 'add_custom_column_data_to_post_table', 10, 2 );
function add_custom_column_data_to_post_table( $column_name, $post_id ) {
if ( '_ct_connection_page_category' === $column_name ) {
$category = get_post_meta( $post_id, '_ct_connection_page_category', true );
echo $category;
}
}
// Make custom column sortable
add_filter( 'manage_edit-oxy_user_library_sortable_columns', 'make_custom_column_sortable' );
function make_custom_column_sortable( $columns ) {
$columns['_ct_connection_page_category'] = '_ct_connection_page_category';
return $columns;
}
add_action( 'pre_get_posts', 'sort_by_custom_column' );
function sort_by_custom_column( $query ) {
if ( ! is_admin() ) {
return;
}
$orderby = $query->get( 'orderby' );
if ( '_ct_connection_page_category' === $orderby ) {
$query->set( 'meta_key', '_ct_connection_page_category' );
$query->set( 'orderby', 'meta_value' );
}
}
// Make custom column filterable
add_action( 'restrict_manage_posts', 'add_custom_column_filter' );
function add_custom_column_filter() {
global $wpdb, $wp_query;
if ( 'oxy_user_library' !== $wp_query->query['post_type'] ) {
return;
}
$categories = $wpdb->get_results( "
SELECT DISTINCT meta_value
FROM $wpdb->postmeta
WHERE meta_key = '_ct_connection_page_category'
ORDER BY meta_value ASC
" );
if ( empty( $categories ) ) {
return;
}
echo '<select name="_ct_connection_page_category">';
echo '<option value="">' . __( 'All Categories', 'text-domain' ) . '</option>';
foreach ( $categories as $category ) {
$selected = isset( $_GET['_ct_connection_page_category'] ) && $_GET['_ct_connection_page_category'] === $category->meta_value ? 'selected' : '';
echo '<option value="' . $category->meta_value . '" ' . $selected . '>' . $category->meta_value . '</option>';
}
echo '</select>';
}
add_filter( 'parse_query', 'filter_by_custom_column' );
function filter_by_custom_column( $query ) {
global $pagenow, $wpdb;
if ( ! is_admin() || 'edit.php' !== $pagenow || 'oxy_user_library' !== $query->query['post_type'] ) {
return;
}
$category = isset( $_GET['_ct_connection_page_category'] ) ? $_GET['_ct_connection_page_category'] : '';
if ( ! empty( $category ) ) {
$query->query_vars['meta_key'] = '_ct_connection_page_category';
$query->query_vars['meta_value'] = $category;
}
}
// Add custom column to post table
add_filter( 'manage_oxy_user_library_posts_columns', 'add2_custom_column_to_post_table' );
function add2_custom_column_to_post_table( $columns ) {
$columns['screenshot'] = __( 'Screenshot', 'text-domain' );
return $columns;
}
// Display screenshot in the custom column
add_action( 'manage_oxy_user_library_posts_custom_column', 'add_screenshot_to_post_table', 10, 2 );
function add_screenshot_to_post_table( $column_name, $post_id ) {
if ( 'screenshot' === $column_name ) {
$year = get_the_time( 'Y', $post_id );
$month = get_the_time( 'm', $post_id );
$screenshot = wp_get_attachment_image_url( $post_id, 'full' );
$files = '/wp-content/uploads/'. $year . '/' . $month . '/' . 'page-screenshot-' . $post_id . '.png';
echo '<img src="' . esc_attr( $files ) . '" alt="Screenshot" style="width:200px; height: 100px; object-fit:contain; bacgkround-color:white; border:1px solid silver; border-radius:4px;">';
}
}