Skip to content

Instantly share code, notes, and snippets.

@nathaningram
Created December 1, 2025 20:38
Show Gist options
  • Select an option

  • Save nathaningram/17d8371ea99c1072e3f7ae41bfee39e1 to your computer and use it in GitHub Desktop.

Select an option

Save nathaningram/17d8371ea99c1072e3f7ae41bfee39e1 to your computer and use it in GitHub Desktop.
Starter Site 2025 - MU Media
<?php
/*
Plugin Name: Custom Media Handling Functions
Plugin URI: https://wpnathan.com
Description: Customize WordPress Default Media Behavior
Version: 1.0
Author: Nathan Ingram
Author URI: https://wpnathan.com
License: GPL2
*/
// Define Custom Image Sizes
if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'News Grid', 600, 400, true );
add_image_size( 'News Large', 1200, 600, true );
}
//////////////////////////////////////////////////////////////////////////////
// Unset Core Image Sizes
// Note: this is a little backwards... commenting out = leaving active
// Use Regenerate Thumbnails to recreate and delete thumbnails
//////////////////////////////////////////////////////////////////////////////
/* WooCommerce Image Sizes */
/*unset( $sizes[ 'woocommerce_gallery_thumbnail' ]);
unset( $sizes[ 'woocommerce_thumbnail' ]);
unset( $sizes[ 'woocommerce_catalog' ]);
unset( $sizes[ 'woocommerce_single' ]); */
//////////////////////////////////////////////////////////////////////////////
// Remove Core Image Sizes
// Note #1: this is a little backwards... commenting out = leaving active
// Note #2: have to comment out in 2 places below
// Note #3: Use Regenerate Thumbnails to recreate and delete thumbnails
//////////////////////////////////////////////////////////////////////////////
// Unregister specific image sizes during the 'init' action
add_action('init', 'bww_unregister_image_sizes', 100);
function bww_unregister_image_sizes() {
// Core Sizes
/* remove_image_size('thumbnail'); */
/* remove_image_size('medium'); */
remove_image_size('medium_large');
/* remove_image_size('large'); */
remove_image_size('1536x1536');
remove_image_size('2048x2048');
// WooCommerce Sizes
/* remove_image_size('woocommerce_gallery_thumbnail'); */
/* remove_image_size('woocommerce_thumbnail'); */
/* remove_image_size('woocommerce_catalog'); */
/* remove_image_size('woocommerce_single'); */
// Gravity Forms Sizes
remove_image_size('gform-image-choice-sm');
remove_image_size('gform-image-choice-md');
remove_image_size('gform-image-choice-lg');
}
// Filter intermediate image sizes to ensure they aren't generated
add_filter('intermediate_image_sizes_advanced', 'bww_remove_image_sizes');
function bww_remove_image_sizes($sizes) {
// Core Sizes
/* unset( $sizes[ 'thumbnail' ]); */
/* unset( $sizes[ 'medium' ]); */
unset($sizes['medium_large']);
/* unset( $sizes[ 'large' ]); */
unset($sizes['1536x1536']);
unset($sizes['2048x2048']);
// WooCommerce Sizes
/* unset( $sizes[ 'woocommerce_gallery_thumbnail' ]); */
/* unset( $sizes[ 'woocommerce_thumbnail' ]); */
/* unset( $sizes[ 'woocommerce_catalog' ]); */
/* unset( $sizes[ 'woocommerce_single' ]); */
/* Gravity Forms Sizes */
unset($sizes['gform-image-choice-sm']);
unset($sizes['gform-image-choice-md']);
unset($sizes['gform-image-choice-lg']);
return $sizes;
}
//Prevent Video File Uploads
function ni_prevent_video_uploads( $file ) {
$filetype = wp_check_filetype( $file['name'] );
$mimetype = $filetype['type'];
// Array of video mime types
$video_mimes = array(
'video/mp4',
'video/quicktime',
'video/mpeg',
'video/ogg',
'video/webm',
'video/3gpp', // 3GP
'video/3gpp2', // 3G2
'video/x-msvideo', // AVI
'video/x-flv', // FLV
'video/h264', // H.264
'video/x-matroska', // MKV
'video/x-ms-wmv' // WMV
// ... add other video mime types if needed
);
// Check if file type is a video
if ( in_array( $mimetype, $video_mimes ) ) {
$file['error'] = 'Sorry, video uploads are not allowed. Please upload to YouTube, Vimeo, or similar streaming service';
}
return $file;
}
add_filter( 'wp_handle_upload_prefilter', 'ni_prevent_video_uploads' );
//Prevent Audio File Uploads
function ni_prevent_audio_uploads( $file ) {
$filetype = wp_check_filetype( $file['name'] );
$mimetype = $filetype['type'];
// Array of audio mime types
$audio_mimes = array(
'audio/mpeg', // MP3
'audio/wav', // WAV
'audio/ogg', // OGG
'audio/x-wav', // WAV
'audio/aac', // AAC
'audio/flac', // FLAC
'audio/x-ms-wma', // WMA
'audio/x-m4a', // M4A
'audio/x-aiff' // AIFF
// ... add other audio mime types if needed
);
// Check if file type is an audio file
if ( in_array( $mimetype, $audio_mimes ) ) {
$file['error'] = 'Sorry, audio uploads are not allowed. Please use an external audio host or contact your website developer for suggestions.';
}
return $file;
}
add_filter( 'wp_handle_upload_prefilter', 'ni_prevent_audio_uploads' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment