Avoid write the full path for images
-
Hi to all,
All the images I have in my wordpress site are taken from “Media” directories and are referenced in this way:
<img src="http://example.com/wp-content/uploads/2011/11/MyImage.jpg">How can I use short reference for all images or files to show something like and “mask” to avoid show the full and real path as follow?
<img src="/images/MyImage.jpg">Thanks in advance for any help.
-
Don’t even try to use relative urls in WordPress. They rarely – if ever – work properly.
As far as I know, you can’t.
The first problem in using a relative path is in deciding what the path should be relative to. Essentially, a WordPress site is just one page with everything else getting included into that page or getting called as required. Since this process is dynamic it’s almost impossible to decide the question, “Relative to what?” And, of course, that’s precisely why WordPress uses absolute paths.
Secondly, you need to account for the directory structure used for uploads (yyyy/mm).
Of course you can cut down on the code needed to obtain the media directory by using
wp-upload_dir($time). See the Codex article for details.Relative paths work in CSS, BTW, but I take it that you’re not talking about background images.
HTH
PAE
Thanks for your answers, I’ve was trying using something like below but I think because of the “year/month/” estructure is not working or I don’t know if this method works in all situations or I need to set something else.
<img src="<?php bloginfo('template_directory'); ?>/images/mainimage.jpg" title="" alt="" /> Reference: http://www.brightcherry.co.uk/scribbles/2008/09/19/wordpress-get-image-path-relevant-to-theme/Thanks again
That should work fine in a parent theme but won’t port to a child theme. Better to use
<?php echo get_stylesheet_directory_uri(); ?>/images/mainimage.jpgfor maximum portability.The code you posted will only work if your images are in a directory called ‘images’ which is a sub-directory of your (parent) theme’s directory. This is fine for static images that are a part of the theme: but obviously you have to put the images in the correct folder yourself, using your favourite FTP client or whatever.
If you are using a child theme, you need to use
bloginfo('stylesheet_folder')/images/your_image.jpeg, assuming once again that you put your images in a directory called images, inside your child theme’s directory.HTH
PAE
Sorry esmi. We’re posting at the same time and saying the same thing. You’re saying it better than me, so I’ll leave it to you.
🙂
PAE
Well, 2 voices are said to be better than 1. 🙂 Plus you also made a good point about the fact that it depends upon how you are using these images in a theme or site.
Thanks both for the answers: Some doubts:
1-) Parent theme is the default theme?
2-) Child theme is any other theme we install?
3-) In “bloginfo(‘stylesheet_folder’)” stylesheet_folder format is like “example.com/wp-content/MyImagesFolder” or is the full URL?
4-) Inside “get_stylesheet_directory_uri()” I need to put the full URL like “http://example.com/wp-content/MyImagesFolder”?Thanks for your help so far.
1 & 2) No. Some themes (called “child themes”) cannot run without their “parent” also being installed. See child themes for more details.
3) No. It’s generates example.com/wp-content/themes/my_standalone_theme
4) No. You use it “as is”. It will generate example.com/wp-content/themes/my_standalone_theme OR example.com/wp-content/themes/my_child_theme depending upon what theme you are currently using.
For full details:
Codex article on
bloginfo()Codex article on
get_stylesheet_directory_uri()HTH
PAE
Thannnnnnnnnks esmi and peredur for your great help!!!
How can I test the outputs?
Inserting in a widget something like this doesn’t show anything to me
<html> <h1><?php bloginfo('name'); ?></h1> <a href="<?php bloginfo('url'); ?>" title="<?php bloginfo('name'); ?>"><?php bloginfo('name'); ?></a> </html>Putting something in
<html>tags means you’re writing a whole HTML page, not just a snippet.Your theme probably already uses
bloginfo('name')to get your blog’s name in the header, so you can see its output. And it probably usesbloginfo('url')to get the URL for your home page. Look in your header.php file and you’ll probably see them used there.I don’t really see why you need to test them like this, though. The Codex tells you exactly what they’ll return.
Cheers
PAE
Thanks peredur,
Thanks for your help. Yes, I saw the header.php and I can see it uses that commands you say. I only want to try because I like to test for myself and I’m not sure if it is a parent theme or child theme.
I only want to know if it possible to see the output in my case in the page or somewhere to be sure about it.
Thanks again
Unlikely to be a child theme unless it’s advertised as such. However if you read the article that esmi pointed you to, you’ll see that a child theme points to its parent in the comment block at the top of the style.css file. A parent theme doesn’t, see this Codex article. Note that the comment block has no
Template:entry for a parent theme.Usually child themes are themes you create yourself based on a theme you want to modify. They are the only recommended way of modifying a theme. It’s not a good idea to modify themes directly unless you control the theme’s code completely (i.e. you wrote the theme) or unless you’re prepared to not update themes and possibly risk security weaknesses.
BTW, I don’t think the text widget will produce any output from PHP. I think it just takes text or HTML. So if you want to test the output, you’ll have to put it in a template file (like header.php), or write your own widget.
HTH
PAE
I was asking hoe to test the output because of the examples they show in the page you shared to me
<h1><?php bloginfo('name'); ?></h1> Ref: http://codex.wordpress.org/Function_Reference/bloginfoThanks for your help both and info shared. I’ll try to do more tests, because I’m not have yet the correct output using that php code. I hope to get it after few tries.
Thanks again for your great help.
Best regards
The topic ‘Avoid write the full path for images’ is closed to new replies.