Adding a watermark to images helps protect your content from unauthorised use, ensuring that your photos or graphics retain attribution even when shared online. In this guide, we’ll walk you through how to automatically apply a watermark to images during upload in WordPress using custom code.
Make sure to add any customizations to a child theme to prevent changes from being lost during theme updates.
Why Add a Watermark to Images?
Watermarking images offers several benefits:
- Protect Intellectual Property: Prevent unauthorised reuse of your images.
- Maintain Attribution: Ensure credit is given to your brand when your images are shared online.
- Brand Visibility: Increase brand awareness by adding your logo to every image.
Step 1: Create the Watermark Image
First, you’ll need a transparent PNG file for your watermark (e.g., your logo or brand name). Upload the watermark image to your WordPress media library or place it in the theme directory.
Example:
- File path: wp-content/themes/your-child-theme/watermark.png
You may also upload to the wp-contents as a normal media file and call the image from there.
Step 2: Add Code to Apply the Watermark
Add the following code to your theme’s functions.php file or in your child theme.
/* Snippet: How to Add a Watermark to Images During Upload in WordPress – 2025
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1597
* Tested with WooCommerce 9.3.3
* “This function adds a watermark to images during upload in WordPress”
*/
function wcsuccess_add_watermark_to_image( $metadata, $attachment_id ) {
$upload_dir = wp_upload_dir();
$image_path = $upload_dir[‘basedir’] . ‘/’ . $metadata[‘file’];// Load the uploaded image and watermark
$image = imagecreatefromstring( file_get_contents( $image_path ) );
$watermark = imagecreatefrompng( get_template_directory() . ‘/watermark.png’ );
// Alternative if in media uploads directory
// $watermark = imagecreatefrompng( WP_CONTENT_DIR. ‘/uploads/2024/10/watermark.png’ );// Get dimensions of both images
$image_width = imagesx( $image );
$image_height = imagesy( $image );
$watermark_width = imagesx( $watermark );
$watermark_height = imagesy( $watermark );// Calculate position to place the watermark (bottom right)
$x = $image_width – $watermark_width – 10;
$y = $image_height – $watermark_height – 10;// Merge the watermark onto the image
imagecopy( $image, $watermark, $x, $y, 0, 0, $watermark_width, $watermark_height );// Save the watermarked image
imagejpeg( $image, $image_path, 100 );// Free memory
imagedestroy( $image );
imagedestroy( $watermark );return $metadata;
}
add_filter( ‘wp_generate_attachment_metadata’, ‘wcsuccess_add_watermark_to_image’, 10, 2 );
How This Works
- Image and Watermark Loading: Loads both the uploaded image and the watermark from the specified paths.
- Positioning the Watermark: Places the watermark at the bottom-right corner, leaving a 10-pixel margin.
- Image Saving: Saves the watermarked image back to the original file path.
- Memory Management: Frees memory to ensure optimal server performance.
Step 3: Validate the Image Upload Process
Test your code by uploading a new image to the WordPress media library. Verify that the watermark appears on the uploaded image. If not, ensure the watermark PNG path is correct.
Best Use Cases for Watermarking
- Photographers and Artists: Protect your work from unauthorised use by adding your brand watermark.
- Online Stores: Use watermarks to prevent competitors from copying product images.
- Bloggers and Marketers: Ensure your brand is visible when images are shared on social media.
Conclusion
Adding a watermark to images during upload is a practical way to protect your content and maintain brand visibility. With the code provided, you can automate the watermarking process without relying on plugins.
Note : Make sure to test the code in a staging environment before applying it to your live site. Use a child theme to safeguard your changes from future theme updates. For further WordPress customisations, explore our wp-config generator for easier configuration management.