Here's the deal: My plugin automatically watermarks images from the WP gallery on the fly using the GD library wherever these images appear. So, I have a physical copy (in wp-uploads) of these attachments, and also a watermarked, dynamic copy of the image which I replace the image source on the page using Simple HTML DOM.

So now, I have an image on my page that points to a URL like this:


What I want to do is make the image source APPEAR like it is the original version, but display the dynamic image instead. I've attempted to mess around with rewrite rules, but it never seems to do anything, no matter what I try. The rules get added to .htaccess, but nothing changes. Here's what I was messing around with:

add_action('generate_rewrite_rules', array( $this, 'addRewrites' ) );

public function addRewrites( $content ) {
    global $wp_rewrite;
    $new_rules = array(
        'img/(.*)'      => plugins_url( 'image.php', __FILE__ ) . '?etc=$1',
    $wp_rewrite->non_wp_rules += $new_rules;

Now if I visit an image URL or a page with an image, NOTHING happens. =/ I'm a newbie when it comes to rewrite rules, so this is a bit beyond me.

  • Why not just generate watermarked images statically? Piping images through PHP has kinda poor track record in WP context...
    – Rarst
    Nov 18, 2012 at 15:18
  • I could watermark the images while they are being uploaded, but then every image that was uploaded before the plugin was active won't be changed at all. I'm trying to make this plugin a solution for all images.
    – Jared
    Nov 18, 2012 at 15:34

2 Answers 2


I am not sure about complete scope of your requirements, but I would probably:

  1. Register special image size(s) that would be used instead of original.
  2. Tie watermarking to generation of that size.

This way there is no need for dynamic image processing. As for your concern about older images having no watermark - you will be able to (re)generate sizes for them using one of the plugins floating around (that is quite common operation).


I ended up changing my code to this, which is what I needed:

Hook in:

add_action( 'generate_rewrite_rules', array( $this, 'addRewrites' ) );

Create the rules:

public function addRewrites( $content ) {
    global $wp_rewrite;

    $plugin_path = substr( str_replace( home_url(), '', plugins_url( '', __FILE__ ) ), 1 );
    $upload_path = substr( str_replace( home_url(), '', wp_upload_dir()['baseurl'] ), 1 );

    $new_rules = array(
        $upload_path . '/*(.+\.jpg)$' => $plugin_path . '/image.php?original=$1',
        $upload_path . '/*(.+\.gif)$' => $plugin_path . '/image.php?original=$1',
        $upload_path . '/*(.+\.png)$' => $plugin_path . '/image.php?original=$1',

    $wp_rewrite->non_wp_rules += $new_rules;

And this only does images from the uploads folder so it is pretty safe I believe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.