get_post_types() as $type ) { // Use the latest filter late as some plugins purely overwrite what's done by others :( // Specific case for media. add_filter( 'manage_' . ( 'attachment' == $type ? 'upload' : 'edit-' . $type ) . '_columns', array( $this, 'add_post_column' ), 100 ); add_action( 'manage_' . ( 'attachment' == $type ? 'media' : $type . '_posts' ) . '_custom_column', array( $this, 'post_column' ), 10, 2 ); } } /** * Adds custom columns in posts, pages, categories and tags tables. * * @since 1.0 * * @param string[] $columns List of table columns. * @param string $before The column before which we want to add our fields. * @return string[] Modified list of columns. */ protected function add_column($columns = [], $before = '') { if ($n = array_search($before, array_keys($columns))) { $end = array_slice( $columns, $n ); $columns = array_slice( $columns, 0, $n ); } foreach ($this->get_custom_columns() as $name => $title) { $columns[ $name ] = esc_html( $title ); } return isset( $end ) ? array_merge( $columns, $end ) : $columns; } /** * Adds the custom columns ( before the comments column ) in the posts, pages and media library tables. * * @since 1.0 * * @param string[] $columns List of posts table columns. * @return string[] Modified list of columns. */ public function add_post_column($columns = []) { return $this->add_column( $columns, 'author' ); } /** * Get post_types. * * @since 1.0 * * @return string[] list of post_types. */ public function get_post_types() { $args = array( 'public' => true ); return get_post_types( $args ); } /** * Get custom columns. * * @since 1.0 * * @return string[] list of columns. */ public function get_custom_columns() { $columns = array( 'custom_name' => 'Name' ); return $columns; } /** * Fills the custom columns in the posts, pages and media library tables * take care that when doing ajax inline edit, the post may not be updated in database yet * * @since 1.0 * * @param string $column Column name * @param int $post_id * @return void */ public function post_column($column = '', $post_id = 0) { // $inline = wp_doing_ajax() && isset( $_REQUEST['action'], $_POST['inline_lang_choice'] ) && 'inline-save' === $_REQUEST['action']; // phpcs:ignore WordPress.Security.NonceVerification // $lang = $inline ? $this->model->get_language( sanitize_key( $_POST['inline_lang_choice'] ) ) : $this->model->post->get_language( $post_id ); // phpcs:ignore WordPress.Security.NonceVerification if (false === strpos($column, 'custom_') || $post_id == 0) { return; } if (strpos($column, 'meta_')>-1) { $value = get_post_meta($post_id, substr($column, 12), true); } else { $p = get_post( $post_id ); $key = 'post_' . substr($column, 7); $value = isset($p->$key) ? $p->$key : ''; } if ( empty( $value ) ) { return; } echo esc_html( $value ); } /** * Hides the column for the custom fields. * * @since 2.7 * * @param string[] $hidden Array of hidden columns. * @return string[] */ public function hidden_columns( $hidden ) { $hidden[] = 'custom_name'; return $hidden; } /** * Can't hide these for they are special. * * wp-admin/includes/class-wp-screen.php * * Line 1164 in function `render_list_table_columns_preferences`; * */ function get_special_columns() { $special = array( '_title', 'cb', 'comment', 'media', 'name', 'title', 'username', 'blogname' ); return $special; } }