Skip to content

Instantly share code, notes, and snippets.

@Gkiokan
Last active March 13, 2017 11:13
Show Gist options
  • Save Gkiokan/14a5cb56e7260984c30ab034ccf521ee to your computer and use it in GitHub Desktop.
Save Gkiokan/14a5cb56e7260984c30ab034ccf521ee to your computer and use it in GitHub Desktop.
Add Custom Columns to WP Admin List
/*
Snipped: Create Custom Post Extension
Author: Gkiokan Sali
Author URL: https://www.gkiokan.net
Date: 13.03.2017
Tested on WP: 4.7.3
License: MIT
Description: Extend your Admin List Table by custom columns in a easy way.
Just call the Class in your Plugin with PostcolumnExtension($YourPostType) and you are fine to go.
The Plugin will hook into the manage_*_posts_columns and will render the specified Column Content,
- in this case the Thumbnail. Feel free to update the code or yourself.
*/
class PostColumnExtension {
protected $post_type = null;
public function __construct($pt=null){
$this->post_type = $pt;
add_action('manage_' . $pt . '_posts_columns', [$this, 'posts_columns'], 5);
add_action('manage_' . $pt . '_posts_custom_column', [$this, 'posts_custom_columns'], 1, 2);
}
/*
Adds Thumbs to the Admin List
*/
public function posts_columns($defaults){
$defaults['wps_post_thumbs'] = __('Beitragsbild');
return $defaults;
}
/*
Create the Column Content
*/
public function posts_custom_columns($column_name, $id){
$c = $column_name;
if($column_name === 'wps_post_thumbs'){
echo "<img src='". get_the_post_thumbnail_url($id) . "' style='width:100%; height:auto; max-width: 140px;'>";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment