feed me feed me chillireward

Article (in detail)

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:\apacheserver\base” 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" />

Popularity: 26% [?]

Page copy
protected against web site content infringement by Copyscape

Liked this article? Subscribe to the article feed via feedburner. Dont want to be hassled by feeds, you can Subscribe by E-mail. Want to know who said what? Get my comments feed. Dont know what a RSS feed is? Read about RSS feeds.

Other Posts for you to Enjoy

Feedback Please!

Note: Your comments may take time to appear, as they are moderated for the first time..

All comments are followed.

 
 
Security Image