One cannot call an image directly in wordpress in your theme files. For instance I cannot actually place an image with the command
When the page is displayed, nothing happens. This is because of WP taking into account the complete URL and not just referential paths. By providing the correct URL, it would make the task simple. The function getpath(); gets the full base path using the builtin wordpress “TEMPLATEPATH” function. It then substitutes the base path with the URL.
Plugin Name: Getpath
Version: 1.0
Description: Gets actual path and replaces it with correct url path.
Author: Lakshmi Mareddy
Author URI: http://www.chilligavva.com/about
*/
function getpath(){
$path=TEMPLATEPATH;
/*
‘/home/public_html/<sitename.com>’ is the base url of your blog.
To determine your base url, save the following code in your WP theme directory, in your header.php after declaration and view your site. Copy the code you see in red, and place it within the quotes of the string replace line.
Code to check your base url:
<h1 style="color: red"></h1>
*/
$newloc = str_replace("/home/public_html/<sitename.com>", "http://sitename.com/", $path);
/*~ the following shows correct path string in local server*/
$newloc = str_replace("C:\apacheserver\base\", "http://localhost/", TEMPLATEPATH);
echo $newloc;
}?>
</sitename.com></sitename.com>
Explanation:
The function captures the absolute path generated by “TEMPLATEPATH”, wordpress function into the variable $newloc.
If you notice there are two definitions for the same variable $newloc. Depending on whether its a local server or blog server, the correct combination is loaded into the variable.
The paths explained
Local Sever: If the root directory defined in your local server is located at “c:apacheserverbase” that is what you will write within quotes in the plugin. Note the two left slant lines. They arre escape quotes for the left slant of windows style file location.
Saving into your plugin directory
1. Enter correct base urls for both local and blog servers in the function.
2. Save the code as “getpath.php”and place it in your plugin directory.
3. Activate it via the admin panel of wordpress.
Calling the function
Whenever you want to invoke an image from the /images directory of your theme, you call it thus:
Written By
Lakshmi MareddyFiled Under
Tutorials, WordpressArticle Tags
get path , Wordpress TweaksTrackback URL
http://www.chilligavva.com/2007/11/21/get-path.html/trackback