Change Editor Default Image Size

add_filter( ‘block_editor_settings_all’, function ( $settings, $context ) { // The default image size when added in the block editor. $settings[‘imageDefaultSize’] = ‘full’; return $settings; }, 10, 2 );Continue reading

Default Featured Image

// Go to Settings > Media after activating this snippet to set the default featured image. add_action( ‘admin_init’, function() { register_setting( ‘media’, ‘default_featured_image’, ‘absint’ ); add_settings_field( ‘default_featured_image’, __( ‘Default Featured Image’, ‘wpcode-snippet’ ), function() { wp_enqueue_media(); $image_id = get_option( ‘default_featured_image’,…Continue reading

Disable Thumbnail Image Sizes

add_filter( ‘intermediate_image_sizes_advanced’, function( $sizes ) { // Disable specific thumbnail sizes, uncomment the ones you want to disable. // unset( $sizes[‘thumbnail’] ); // 150px x 150px // unset( $sizes[‘medium’] ); // 300px x 300px // unset( $sizes[‘medium_large’] ); // 768px…Continue reading

Add Auto Sizes to Lazy Loaded images

add_filter( ‘wp_get_attachment_image_attributes’, function( $attr ) { if ( ! isset( $attr[‘loading’] ) || ‘lazy’ !== $attr[‘loading’] || ! isset( $attr[‘sizes’] ) ) { return $attr; } // Skip if attribute was already added. if ( false !== strpos( $attr[‘sizes’], ‘auto,’…Continue reading

Allow Contributors to Upload Images

add_filter( ‘user_has_cap’, function ( $allcaps, $caps, $args, $user ) { if ( in_array( ‘contributor’, $user->roles ) && empty( $caps[‘upload_files’] ) ) { $allcaps[‘upload_files’] = true; } return $allcaps; }, 10, 4 );Continue reading

Limit Uploaded Image Size

add_filter( ‘wp_handle_upload’, function ( $file ) { $max_width = 1920; $max_height = 1920; // Check if the file is an image. $mime_type = mime_content_type( $file[‘file’] ); if ( strpos( $mime_type, ‘image’ ) === false ) { return $file; } //…Continue reading

Add Media File Size Column

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; }…Continue reading