// Disable Auto Generated Images Sizes
add_action('intermediate_image_sizes_advanced', 'disable_image_sizes');
function disable_image_sizes($sizes) {
//unset($sizes['thumbnail']);
//unset($sizes['medium']);
//unset($sizes['large']);
unset($sizes['medium_large']);
unset($sizes['1536x1536']);
unset($sizes['2048x2048']);
return $sizes;
}
add_action('init', 'disable_other_image_sizes');
function disable_other_image_sizes() {
remove_image_size('post-thumbnail');
remove_image_size('another-size');
}
add_filter('big_image_size_threshold', '__return_false');
Media
Add media file size column
Display the file size in a separate column in the Media screen
add_filter( 'manage_upload_columns', function ( $columns ) {
$columns ['file_size'] = esc_html__( 'File size' );
return $columns;
} );
add_action( 'manage_media_custom_column', function ( $column_name, $media_item ) {
if ( 'file_size' !== $column_name || ! wp_attachment_is_image( $media_item ) ) {
return;
}
$filesize = size_format( filesize( get_attached_file( $media_item ) ), 2 );
echo esc_html( $filesize );
}, 10, 2 );
Add Featured Image column
add_filter( 'manage_posts_columns', function ( $columns ) {
// You can change this to any other position by changing 'title' to the name of the column you want to put it after.
$move_after = 'title';
$move_after_key = array_search( $move_after, array_keys( $columns ), true );
$first_columns = array_slice( $columns, 0, $move_after_key + 1 );
$last_columns = array_slice( $columns, $move_after_key + 1 );
return array_merge(
$first_columns,
array(
'featured_image' => __( 'Featured Image' ),
),
$last_columns
);
} );
add_action( 'manage_posts_custom_column', function ( $column ) {
if ( 'featured_image' === $column ) {
the_post_thumbnail( array( 300, 80 ) );
}
} );
Media library Alt text column in list view
function add_alt_text_column($columns) {
$columns['alt_text'] = 'Alt Text';
return $columns;
}
add_filter('manage_media_columns', 'add_alt_text_column');
// Display alt text with colored dot in the custom column
function display_alt_text_column($column_name, $attachment_id) {
if ($column_name === 'alt_text') {
$alt_text = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
$file_type = wp_check_filetype(wp_get_attachment_url($attachment_id));
$image_types = array('jpg', 'jpeg', 'png', 'webp', 'avif', 'gif', 'svg');
if (in_array($file_type['ext'], $image_types)) {
if (!empty($alt_text)) {
echo esc_html($alt_text);
} else {
echo '<div style="display:flex;align-items:center;"><p style="margin:0;color: red;font-size: 48px;">•</p><p style="margin-top:1.1rem;padding-left:1rem;font-weight:700;">Missing Alt text!</p></div>';
}
} else {
echo '<div style="display:flex;align-items:center;"><p style="margin:0;color: royalblue;font-size: 48px;">•</p><p style="margin-top:1.1rem;padding-left:1rem;font-weight:700;">N/A</p></div>';
}
}
}
add_action('manage_media_custom_column', 'display_alt_text_column', 10, 2);
Show post thumbnails in RSS Feed
function snipjar_feed_post_thumbnail($content) {
global $post;
if(has_post_thumbnail($post->ID)) {
$content = '' . $content;
}
return $content;
}
add_filter('the_excerpt_rss', 'snipjar_feed_post_thumbnail');
add_filter('the_content_feed', 'snipjar_feed_post_thumbnail');
Allow contributors to upload images
if ( current_user_can('contributor') && !current_user_can('upload_files') )
add_action('admin_init', 'allow_contributor_uploads');
function allow_contributor_uploads() {
$contributor = get_role('contributor');
$contributor->add_cap('upload_files');
}
Disable attachment pages
add_action(
'template_redirect',
function () {
global $post;
if ( ! is_attachment() || ! isset( $post->post_parent ) || ! is_numeric( $post->post_parent ) ) {
return;
}
// Does the attachment have a parent post?
// If the post is trashed, fallback to redirect to homepage.
if ( 0 !== $post->post_parent && 'trash' !== get_post_status( $post->post_parent ) ) {
// Redirect to the attachment parent.
wp_safe_redirect( get_permalink( $post->post_parent ), 301 );
} else {
// For attachment without a parent redirect to homepage.
wp_safe_redirect( get_bloginfo( 'wpurl' ), 302 );
}
exit;
},
1
);