Creating a WordPress plugin that compresses images requires some coding knowledge in PHP, JavaScript, and CSS. In this example, we will create a simple plugin that uses the free open-source image optimization library called reSmush.it to optimize and compress images in WordPress.
-
Start by creating a new folder in your WordPress "wp-content/plugins" directory. Name it "image-compression-plugin" or any name you prefer.
-
Inside this folder, create a new file named "image-compression-plugin.php" and open it in a text editor.
-
Add the following PHP code at the top of the file to define your plugin's basic information:
<?php
/*
Plugin Name: Image Compression Plugin
Plugin URI: https://github.com/my-plugin/image-compression-plugin
Description: Optimizes and compresses images using the reSmush.it API.
Version: 1.0
Author: Your Name
Author URI: https://www.yourwebsite.com
License: GPL2
*/
- Add the "register_activation_hook" and "register_deactivation_hook" functions to enable and disable the plugin:
register_activation_hook( __FILE__, 'image_compression_plugin_activate' );
register_deactivation_hook( __FILE__, 'image_compression_plugin_deactivate' );
function image_compression_plugin_activate() {
// Activation action
}
function image_compression_plugin_deactivate() {
// Deactivation action
}
- Include the required filters and actions to hook into WordPress media upload process:
add_filter( 'wp_handle_upload_prefilter', 'image_compression_plugin_prefilter' );
add_filter( 'wp_handle_upload', 'image_compression_plugin_handle_upload' );
function image_compression_plugin_prefilter( $file ) {
// Check if the file is an image
if ( substr( $file['type'], 0, 5 ) !== 'image' ) {
return $file;
}
// Set a flag to process the image after uploading
global $image_compression_plugin_image;
$image_compression_plugin_image = true;
return $file;
}
function image_compression_plugin_handle_upload( $fileinfo ) {
// Check if we should process the image
global $image_compression_plugin_image;
if ( !$image_compression_plugin_image ) {
return $fileinfo;
}
// API settings
$api_url = "http://api.resmush.it/";
// Check if the uploaded file is an image
if ( substr( $fileinfo['type'], 0, 5 ) !== 'image' ) {
return $fileinfo;
}
// Build request
$data = array(
'files' => new CURLFile( $fileinfo['file'] )
);
// Initialize cURL
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $api_url );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
// Call reSmush.it API
$response = json_decode( curl_exec( $ch ) );
curl_close( $ch );
// Check the API response
if ( isset( $response->dest ) && $response->dest ) {
// Save the optimized image
$optimized_image = file_get_contents( $response->dest );
file_put_contents( $fileinfo['file'], $optimized_image );
// Update the file size
$fileinfo['size'] = $response->src_size;
}
return $fileinfo;
}
-
Save the "image-compression-plugin.php" file and upload the entire "image-compression-plugin" folder to the "wp-content/plugins" directory of your WordPress website.
-
Log in to your WordPress admin panel, go to "Plugins," find your new "Image Compression Plugin," and click "Activate."
Now the plugin will automatically compress uploaded images during the media upload process using the reSmush.it API. Customize the API URL, supported formats, and options to enhance the functionality of the plugin as required.