Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

What I'm trying to do is very simple.

Whenever someone visits, say site.com/socialproof.png, I want it to use a rewrite rule to point it to a file in my plugins folder.

Can I do this with WP's rewrite rules or do I need to manually add it to the .htaccess file? What would I use as the rewrite rule to do this? Thanks!

share|improve this question
    
Yes you can do it using rewrite rules. But, why not upload it to media and use that URL instead? –  Rutwick Gangurde Mar 7 '12 at 4:46
    
@RutwickGangurde It is a dynamically generated PNG file created using imagettftext() and similar functions. –  Jared Mar 7 '12 at 4:54
    
Also, the original file that it points to has such a long URL that I need to make it shorter since the plugin will be distributed. I don't want users to have to do something like this: <img src="http://site.com/wp-content/plugins/myplugin/etc/blah.php" />, that would just be wrong. :) –  Jared Mar 7 '12 at 5:02
    
You're right! What about the name of the file, is it gonna change too? –  Rutwick Gangurde Mar 7 '12 at 5:13
    
I tried something, but that way requires a template for the image to be displayed. Let me know if you're interested in knowing. –  Rutwick Gangurde Mar 7 '12 at 5:30

1 Answer 1

up vote 1 down vote accepted

Using .htaccess rewrites wouldn't be a good approach since it would fail when the plugin is disabled.

You also can't rewrite image URLs with WordPress because its native request parsing does not recognize single file names. However, if an image doesn't exist, the web server will redirect the request over to WordPress for handling. This allows us to hook in and include your dynamic image template.

add_action('init', 'wpse_44612_init', 0);
function wpse_44612_init(){
    if(preg_match('/socialproof\.png/', $_SERVER['REQUEST_URI'])){
        require_once(ABSPATH.'/wp-content/plugins/SocialProof/classes/SocialProofMakeImage.php');
        die();
    }
}

With this approach, your URL appears as a static image, but is dynamically generated via your PHP class file.

You can download the plugin file is here.

share|improve this answer
    
Thanks Brian, one thing I would like to mention after testing this out a bit, it doesn't seem to work with the default permalinks. Do you know if there's an easy way around that? –  Jared Mar 13 '12 at 20:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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