0

I'm rewriting this question because I realized it was totally unclear what I want to do. I'm looking for a way to "obscure" the theme directory so that absolute URLs that reference things like images and stylesheets in that theme directory don't have the "wp-content/themes/my_theme/" structure, but are rather more direct, like a normal website, eg: "/images/my_image.gif" and "/css/stylesheet.css".

It's easy to hard-code the path to your theme's folder using the recommended get_template_directory_uri() to programmatically bring up the theme folder, but the theme folder structure is completely exposed this way.

I'm hoping that the answer can leverage wp_rewrite in some way, because the only other solution I can think of involves editing .htaccess and that means hard-coding the theme directory into your htaccess file. I suppose this could be automated via a plugin or your theme activation function that edits the htaccess file, but I'm hoping that someone out there knows of a more "built-in" way to do it.

2

Use get_template_directory_uri():

$url = get_template_directory_uri() . '/images/myimage.jpg';

This function will always return the correct path to your theme.

If you want to hide/shorten that URL, you could use an endpoint, maybe img. The downside is, you load the complete WordPress for each image request.

Or use mod_rewrite:

RewriteRule ^img/([a-z\d-])\.png /wp-content/themes/my_theme/img/$1.png [L]

This will not work in a standalone theme.

  • I'm sorry Toscho, I should have been more clear in my OP. Obviously I know about get_template_directory_uri() etc. But that will spit out mysite.com/wp-content/themes/mytheme which is not what I want. Maybe I need to rewrite my OP to be more clear. – Tom Auger May 16 '13 at 1:26
  • @TomAuger I was already wondering what happened to you. :D I made an update to my answer. – fuxia May 16 '13 at 3:55
  • Thanks @Toscho. I realize that my original question was a bit silly, considering that WordPress exposes the theme directory and URI in so many other ways. I really appreciate your link to your other post on Endpoints. Lots to learn there! – Tom Auger May 28 '13 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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