Wordpress 3.8 image path to image folder not working - php

This seems to have been asked around, but with different answers.
I've the image folder "images" in the same directory as the page.php file which I want to add images to. Yet the path "images/foldername/image.gif" doesn't yield any images on the page. Whereas from a non-wordpress html file that path would work, so what is the answer to this? What makes wordpress not understand file paths, assuming I'm not missing something?
also tried /images with the slash just in case.

You may use:
echo get_bloginfo('template_url') . 'images/imagename.png';
Or
<img src="<?php bloginfo('template_url'); ?>/images/img.png" />
bloginfo, get bloginfo. Also, read this article.

You mean inside the WordPress themes folder? If so, correct path is http://www.domain.com/subdirectory/wp-content/themes/yourtheme_name/images
You can use <?php get_template_directory_uri(); ?>/images/foldername/image.gif
get_template_directory_uri()

Related

does using different relative paths for the same resource get cached by the browser just once?

My question might not be clear, so here is an example.
I have a PHP script that will auto add the relative path to all of HTML resources like
CSS - <link href href="<?php echo $siteroot ?>css/main.css" ... >
JS - <script src="<?php echo $siteroot ?>js/main.js"</script>
Images <img src="<?php echo $siteroot ?>img/avatar.jpg" ... >
other uses like PHP includes
the script will auto make the relative path to the site root and this will vary to be '../', '../../', '../../../', or an empty string '' if it is the site root folder - main index
My question does this will affect the cache system the browser uses? I thought of this because the same resource will be different in many pages that are in subfolders!
once ../../img/avatar.jpg other ../img/avatar.jpg, other img/avatar.jpg alone!
I have tried with chrome and run a file with img and then deleting the img and opening a file in a subfolder. This worked and the image was there!
I still not sure and want a granted answer about the caching process for relative paths. If there is any extra information, I will appreciate it :)
Thank you
These resources will be cached once because absolute path is same, regardless they relative paths differ. Browsers uses absolute paths for caching.

Pointing to a directory in Wordpress

I am attempting to integrate a Wordpress blog page into my (php) website. So far I have been successful in creating an installing it into a subdirectory. I can see that this works.
The issue I am facing is that images are not showing (the alt text is). The issue is that Wordpress uses a incorrect path to the flies:
Wrong Path: https://sitename.com/wordpress/blog/images/brand/logo.png
Correct: https://sitename.com/images/brand/logo.png
It should not have /wordpress/blog/in it.
How do I go about resolving this to point at the correct directory without duplicating the files. Note that in the code the path is is written as: /images/brand/logo.png
If your image folder lays inside theme directory, then you can use
<img src="<?php bloginfo('template_url'); ?>/images/brand/logo.png" />
If your image folder lays outside the theme directory, then you may use
<img src="<?php echo home_url(); ?>/images/brand/logo.png" />
Put this in your htaccess in your root directory.
RewriteEngine On
RewriteRule ^wordpress/blog/images/(.*)$ /images/$1 [L]
That should rewrite those urls.
Depending on your needs, you may also want to consider this article. It doesn't sound like your problem is that you want to change all urls, only the urls to your images.
If you already have path as /images/brand/logo.png , just go 2 levels up like this;
../../images/brand/logo.png
If you are using a wordpress default theme/img upload folder then wordpress build in function get_stylesheet_directory() would work for you. In this case no matter where you shift your project it will always grab this path, this much better then hardcoding path every time.

php:src path in included files not rendering properly

I have created a php file that consists path say rootdir/level1/level2/sample.jpg Now i want to include this file from any where i.e., from any level. how to do it. So that i can get image in any level of path.
Try this.
<img src="<?php echo $_SERVER['SERVER_NAME'];?>/level1/level2/sample.jpg">

PHP Fix Relative Image Path Using .htaccess

I have a php file, for example www.example.com/folder/test.php
The file above (test.php) has the line
include($_SERVER['DOCUMENT_ROOT'].$page);
$page = /folder2/.$directory1./.$directory2./.index.php
$directory1 and $directory2 are all random.
However,
The index page has an image on it that just references the image as the source.
For Example src="image.bmp"
So what happens is the image's url actually shows up like this...
www.example.com/folder/image.bmp
And it should show up like this...
www.example.com/folder2/.$directory1./.$directory2./image.bmp
Is there a way to fix this besides using absolute paths?
There are millions of directories and we would like to correct the issue rather than correct the problem (even though we will do both).
Can I place an htaccess file in the directory its looking for the image in to redirect it?
FROM: www.example.com/folder/
TO: www.example.com/folder2/.$directory1./.$directory2./
If you want to do that, you should either dynamically change the .htaccess or specify in it any possible $directory1/$directory2 combination. In my opinion, the best way to do this is to change the src of that image in the file index.php to something like:
<img src="/folder2/<?php echo $directory1; ?>/<?php echo $directory2; ?>" />

wordpress image path changing dependent on file

I'm having problems with the paths to my first wordpress theme. Post images and stuff not related to css is located in wordpress_folder/blog-images/ and in index.php when I link to images I use this path: blog-images/img.jpg
The problem is now that when I want to link to the same image from another file (not index.php) in this case single.php wich displays one blog post, the correct path is now ../../../blog-images/img.jpg
This is causing problems in the includes like sidebar etc. sidebar.php works fine when called from index.php but the images path is changed if sidebar.php is called from single.php.
Does anyone know what's going on?
If you are creating these links from within php scripts, I would suggest using the site_url() function to get the URL for your wordpress install and then appending your images path to the end of that. If you are editing static theme files like css, then you should use /wordpress_folder/blog_images/img.jpg.
Something like <img src="<?php echo site_url() ?>/blog_images/img.jpg" /> should be sufficient from theme files.
The reason that paths are chaning is because if you are in wordpress_folder then the path blog_images/img.jpg resolves to wordpress_folder/blog_images/img.jpg but if you are on a post that has the url yoursite.com/wordpress_folder/2011/09/category/my_great_post then the path would resolve to wordpress_folder/2011/09/category/blog_images/img.jpg which is obviously incorrect.
For this reason you should try to use the absolute path or full URL so that no matter what file/folder/url you are linking from, the path will always be correct.
The main downside you may run into is that if you were to change the name of your wordpress folder, or remove it altogether, then you may need to make a lot of edits to reflect that. But in any case, you should put the / in front of your path so that it can be referenced the same from everywhere.
Also check out the site_url() reference page, it lists some other helpful functions at the bottom that may be useful to you.
I thought this was a little unclear from drew's answer, so I am adding a little bit more in a separate answer. His advice is sound and I agree with him.
If you prepend a url with a / then it will navigate based on your site url. Without the slash it uses relative navigation.
So here are some examples for www.mydomain.com
//always shows the image located at http://www.mydomain.com/myfolder/pic.png
//no matter what the url is
<img src="/myfolder/pic.png" />
//shows the image located relative to the current path
//if current url is http://www.mydomain.com/posts/ then the image will come from
//http://www.mydomain.com/posts/myfolder/pic.png
<img src="myfolder/pic.png" />
If you are creating links dynamically from php side then you will want to use site_url().
If you are creating links to your theme directory folder then you will want to use bloginfo('template_directory')

Categories