Skip to content

How to add a sortable custom filename column to the WordPress media library admin column

black and silver laptop computer
//add a little css if you like
function enym_admin_table_css() {
  echo '<style>
    p.filename { display: none; } 
  </style>';
}
add_action('admin_head', 'enym_admin_table_css');

// Add the column
function filename_column( $cols ) {
	$cols["filename"] = "Filename";
	return $cols;
}

// Display filenames
function filename_value( $column, $post_ID ) {
	if ( 'filename' === $column ) {
		$meta = wp_get_attachment_metadata( $post_ID );
		echo substr( strrchr( $meta['file'], '/' ), 1); // Used a few PHP functions cause 'file' stores local url to file not filename
	}
}

// Register the column as sortable & sort by name
function filename_column_sortable( $cols ) {
	$cols["filename"] = "name";
	return $cols;
}

// Hook actions to admin_init
function hook_new_media_columns() {
	add_filter( 'manage_media_columns', 'filename_column' );
	add_action( 'manage_media_custom_column', 'filename_value', 10, 2 );
	add_filter( 'manage_upload_sortable_columns', 'filename_column_sortable' );
}
add_action( 'admin_init', 'hook_new_media_columns' );

Dieser Beitrag hat 0 Kommentare

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

An den Anfang scrollen