All WordPress snippets
Want your own copy?
Fork this repo and build your own snippet collection. Fork on GitHub →
WordPress
Show a Thumbnail Column in the Post List Table
Adds a small featured-image preview column to the posts list in wp-admin, so you don't have to open each post to see its thumbnail.
php
/**
* Add thumbnail column to post listing
*/
add_image_size( 'admin-list-thumb', 80, 80, false );
function wpcs_add_thumbnail_columns( $columns ) {
if ( !is_array( $columns ) ) {
$columns = array();
}
$new = array();
foreach( $columns as $key => $title ) {
if ( $key == 'title' ) { // Put the Thumbnail column before the Title column
$new['featured_thumb'] = __( 'Image');
}
$new[$key] = $title;
}
return $new;
}
function wpcs_add_thumbnail_columns_data( $column, $post_id ) {
switch ( $column ) {
case 'featured_thumb':
echo '<a href="' . $post_id . '">';
echo the_post_thumbnail( 'admin-list-thumb' );
echo '</a>';
break;
}
}
if ( function_exists( 'add_theme_support' ) ) {
add_filter( 'manage_posts_columns' , 'wpcs_add_thumbnail_columns' );
add_action( 'manage_posts_custom_column' , 'wpcs_add_thumbnail_columns_data', 10, 2 );
}
More WordPress snippets
Register and Output a Custom Nav Menu
Adds a new menu location you can assign in Appearance → Menus, shows how to output it in a template, and includes the multi-menu version too.
Define a Reusable Global String
A simple pattern for storing a value — like a phone number or address — in one place and reusing it anywhere in your theme.
Auto-Escape HTML Typed Inside Code Tags
Makes sure any markup typed inside <code> or <tt> tags in the editor gets escaped automatically, so it displays as visible text instead of being rendered as real HTML.