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

Add file to media library programmatically

$file = ‘/path/to/file.png’; $filename = basename($file); $upload_file = wp_upload_bits($filename, null, file_get_contents($file)); if (!$upload_file[‘error’]) { $wp_filetype = wp_check_filetype($filename, null ); $attachment = array( ‘post_mime_type’ => $wp_filetype[‘type’], ‘post_parent’ => $parent_post_id, ‘post_title’ => preg_replace(‘/\.[^.]+$/’, ”, $filename), ‘post_content’ => ”, ‘post_status’ => ‘inherit’ );…Continue reading

Save images as WEBP (copy)

/** * Convert Uploaded Images to WebP Format * * This snippet converts uploaded images (JPEG, PNG, GIF) to WebP format * automatically in WordPress. Ideal for use in a theme’s functions.php file, * or with plugins like Code Snippets…Continue reading

Smush – Original Images

/** * Plugin Name: [Smush] – Original Images * Plugin URI: https://premium.wpmudev.org/ * Description: Displays all original images so they can be deleted. Requires Smush * Author: Panos Lyrakis @ WPMUDEV * Author URI: https://premium.wpmudev.org/ * License: GPLv2 or later…Continue reading

Save images as WEBP

/** * Convert Uploaded Images to WebP Format * * This snippet converts uploaded images (JPEG, PNG, GIF) to WebP format * automatically in WordPress. Ideal for use in a theme’s functions.php file, * or with plugins like Code Snippets…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

Tasty Pins – Remove the Pin button for an image by URL

(function(callback) { if (document.readyState !== “loading”) { callback(); } else { document.addEventListener(“DOMContentLoaded”, callback); } })(() => { let tp_exclusions = document.querySelectorAll(‘img[src=”https://example.com/wp-content/uploads/2023/12/logo.png”]’); if ( tp_exclusions.length === 0 ) { return; } for( const tp_exclusion of tp_exclusions ) { tp_exclusion.dataset.pinNopin = ‘nopin’;…Continue reading