Last active
April 14, 2024 12:00
-
-
Save nathaningram/c1a7ced7b5a056a301222675d83bcab3 to your computer and use it in GitHub Desktop.
Creating a Starter Site 2022 - Custom Functions - Media Handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Custom Media Handling Fuctions | |
Plugin URI: https://nathaningram.com | |
Description: Customize WordPress Default Media Behavior | |
Version: 2023.11 | |
Author: Nathan Ingram | |
Author URI: https://nathaningram.com | |
License: GPL2 | |
*/ | |
// Security Check | |
if (!defined('ABSPATH')) { | |
die(); | |
} | |
// Unset Core Image Sizes | |
// Note: the code below removes ALL image sizes. Delete or comment out lines for sizes you want to keep. | |
add_filter( 'intermediate_image_sizes_advanced', 'ni_remove_image_sizes' ); | |
function ni_remove_image_sizes( $sizes ) { | |
/* Default WordPress */ | |
unset( $sizes[ 'thumbnail' ]); | |
unset( $sizes[ 'medium' ]); | |
unset( $sizes[ 'medium_large' ]); | |
unset( $sizes[ 'large' ]); | |
unset( $sizes[ '1536x1536' ]); | |
unset( $sizes[ '2048x2048' ]); | |
/* WooCommerce Image Sizes */ | |
unset( $sizes[ 'woocommerce_gallery_thumbnail' ]); | |
unset( $sizes[ 'woocommerce_thumbnail' ]); | |
unset( $sizes[ 'woocommerce_catalog' ]); | |
unset( $sizes[ 'woocommerce_single' ]); | |
return $sizes; | |
} | |
// 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 ); | |
} | |
//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-ms-wma', // WMA | |
'audio/aac', // AAC | |
'audio/flac', // FLAC | |
'audio/mp4', // MP4 Audio | |
'audio/webm', // WebM Audio | |
'audio/amr', // AMR | |
// ... add other audio mime types if needed | |
); | |
// Check if file type is an audio | |
if ( in_array( $mimetype, $audio_mimes ) ) { | |
$file['error'] = 'Sorry, audio uploads are not allowed.'; | |
} | |
return $file; | |
} | |
add_filter( 'wp_handle_upload_prefilter', 'ni_prevent_audio_uploads' ); | |
// Limit file uploads to 10MB | |
function ni_limit_file_upload_size( $file ) { | |
$file_size = $file['size']; | |
// Maximum file size in bytes (10MB) | |
$max_size = 10 * 1024 * 1024; // 10MB in bytes | |
// Check if file size exceeds the limit | |
if ( $file_size > $max_size ) { | |
$file['error'] = 'This file is too large to be uploaded to the Media Library.'; | |
} | |
return $file; | |
} | |
add_filter( 'wp_handle_upload_prefilter', 'ni_limit_file_upload_size' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment