Article Feed

Get Path

One cannot call an image directly in wordpress in your theme files. For instance I cannot actually place an image with the command

<img src="images/imagename" alt="this is an image" border="0" width="300" height="200" />

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:

<img src="http://chilligavva.com/wp-admin/%3C?getpath%28%29;?%3E/images/imgname.jpg" />

Lakshmi Mareddy is a polyglot from South India and is interested in design, technology, UI and human computing. She also is a history buff and an avid reader. If you like the way she writes, why dont you subscribe to the site feed via RSS or Email? Its absolutely free and helps you stay current. You can contact her via the contact page or write to lak [at] mareddy [dot] com.

Share your thoughts

Note: Your comments may take time to appear, as they are moderated for the first time.. All comments are followed. Your Personal Info will not be shared. Still have concerns? Read the rights and privacy details of Chilligavva..

 
Security Image
 
CommentLuv Enabled