WordPress.org

Ready to get started?Download WordPress

Forums

Relative URL for local images request (8 posts)

  1. jblifestyles
    Member
    Posted 4 years ago #

    Can we adjust the image insert function to only insert a relative url instead of a full path to an image file?

    <img src="/wp-content/uploads/filename.jpg" />

    vs

    <img src="http://www.domain.com/wp-content/uploads/filename.jpg" />

    This would make it much easier in the database if/when we change a blog directory, go from working on local machine to live server, etc..etc..

    Thanks to all the developers who make wordpress happen, and I do hope this is something you will consider.

    -Jake

  2. slee
    Member
    Posted 4 years ago #

    any news on this? i would really like this as well

  3. Jason Judge
    Member
    Posted 4 years ago #

    Even <img src="/wp-content/uploads/filename.jpg" /> is still absolute with respect to the domain.

    Better would be to support relative URLs like <img src="wp-content/uploads/filename.jpg" />. That way a site can be developed in a folder and moved up to the root (or to another folder) when deployed.

    This would require either an output filter that modifies these relative URLs depending upon the current page path, or the use of the "base" meta-tag to tell the browser to treat all relative paths wrt the site root.

    I'm going through this myself at the moment, and am use to just being able to move a whole CMS, knowing that all links are relative, so this came as a surprise to me.

    -- Jason

  4. Steven Vachon
    Member
    Posted 4 years ago #

    I agree. I've been waiting for this feature addition for a while. I think it should be built into WordPress and not a plugin/filter.

  5. vangrog
    Member
    Posted 4 years ago #

    Just install some php execution plugin to allow you to insert php codes on your posts (I recommend http://wordpress.org/extend/plugins/php-execution-plugin/). Then simply write your posts using relative paths, such as:

    <img border="0" src="="<?php echo $ROOT; ?>/zzzz/xxxx/mypic.jpg" width="100" height="100">

    or

    <?php echo '<img border="0" src="' . $ROOT . '/zzzz/xxxx/mypic.jpg" width="100" height="100">'; ?>

    you can also use $BASE_URL or bloginfo('url') if you prefer...

  6. Steven Vachon
    Member
    Posted 4 years ago #

    Too messy. The easiest solution, outside of waiting for WordPress to implement such, is to write a filter.

  7. ldexterldesign
    Member
    Posted 4 years ago #

    This needs to happen BADLY. Anyone found a suitable solution?

    I'm starting to think twice before dropping in image placeholders during localhost builds these days because I know I'll have to redo the links every time I update the live site/DB, which is unfortunate :[

    Cheers guys,

  8. Chris Bratlien
    Member
    Posted 4 years ago #

    Hey folks,

    I just made a plugin yesterday for this.

    http://wordpress.org/extend/plugins/relative-site-url-on-content-save/

    It uses a shortcode [url] for bloginfo('url') and then does a content filter to substitute the full URL base with [url] when you click Publish or Update.

    By the time the page renders to your visitors, the URL is absolute again in their browser. So this solution should also work if your bloginfo('url')is http://mysite.com/staging instead of residing at the / root of your webserver's DocumentRoot.

    This plugin needs testers, so please try it out.

    http://wordpress.org/extend/plugins/relative-site-url-on-content-save/

    Or, if you just want to put it in your functions.php, it's only 9 lines of PHP.

    http://chrisbratlien.com/2010/05/16/relative-site-url-on-content-save/

Topic Closed

This topic has been closed to new replies.

About this Topic