Relative path for images/uploads?

  1. Anony Mouse

    While I can see some value in relative paths I have never found it an issue when moving sites between domains.

    My solution is to export the database and then do a global replace on the resulting text file (no SQL required) i.e

    replace 'http://myolddomain.com' with 'http://mynewdomain.com'

    remember to unzip your db export file and open it with a text editor as a plain text (not sql) file. If you try to open the file with any kind of language recognition or specific formatting it will likely take an incredibly long time to open the file or use up all your memory and crash your machine. I recommend notepad for windows and textedit for mac for this job.

    Save the file and then import to your new database. All references to url paths will work without the need to edit each and every post/page.

    Just my 2c.

    Posted: 6 years ago #
  2. I have two functions that will take care of this for you, they're not nearly clean enough to submit to the patch but as a fix you're welcome to them.

    http://thisismyurl.com/6178/replace-absolute-urls-wordpress-database/ will replace the existing database entries with a path relative to your root and http://thisismyurl.com/6166/replace-wordpress-static-urls-dynamic-urls/ will replace paths in new posts with a path from your root.

    If anybody wants to help me clean up the code, I'll happily submit it as a 3.3 patch.

    Posted: 6 years ago #
  3. @ martinhurford

    WOW thanks for that advice Martin, it certainly worked as a workaround to the massive headache I was faced with.

    Many thanks...!

    Posted: 6 years ago #
  4. smjdesign

    The upload directory is not stored in the database, just the relative path inside the upload directory. Use the 'upload_dir' filter to change the URL output of attachments/images.

    Posted: 6 years ago #
  5. another-webmaster


    Our site is having over 18k images & 2200+ pages. The idea already is eaww, just because of not being able to use relative path.
    Isn't there a way to have a setting in .htaccess that could solve this issue?
    Already a topic which started over 4 years ago and it seems it is not solvable or do I miss some solutions. (and no we dont need it for moving over to a new server/provider, but to work with)

    Posted: 6 years ago #
  6. another-webmaster


    As respond on my own posting:

    Following shortcode API could maybe do the trick (at least if you upload images by using FTP).
    I want my image folder in the wp-content folder therefore I use code as shown, ofcourse you can make the folder where you want, just adjust path to it!
    #Create a folder in wp-content folder name it -> img <-
    #Copy this code in functions.php (the one in your theme folder)

    //Shortcode API images
    function myImageLink() {
    	return get_option('home') . '/wp-content/img/';
    add_shortcode('Pics', 'myImageLink');
    // end shortcode images

    #Save functions.php
    Now you can use it in your posts/pages (keep in mind the shortcode is case-sensitive)
    <img src="[Pics]test.png" />
    or if you created a folder in that img folder named -> whynot <-
    <img src="[Pics]/whynot/test.png" />

    To be honest, I have no clue if this harms CEO or other plugins or search engine(s) but I will go for it.
    Creating a plugin with this code shouldn't be to difficult (I can't).
    Note: does this 'trick' work also for you Media library?...ehm .. NO :)
    Tested with WordPress 3.2.1

    Posted: 6 years ago #
  7. Paths should be relative to allow easy migration of the site to a new server, but for RSS content, etc., WP on the local server should generate and display absolute links based on both the relative links and the root domain.

    The length of this thread shows this is a significant issue that is not resolved.

    Posted: 6 years ago #
  8. Bob Gregor

    HTH - when I move sites I run a mySQL find replace:
    update wp_posts set post_content = replace('post_content','http://oldsite.com/wp-content/','http://newsite.com/wp-content/');

    Or do as @martinhurford suggested

    As for why this hasn't been fixed in core, I have no idea except the depth and breadth of this type of change will require extensive testing to make sure all paths are re-written.

    Posted: 6 years ago #
  9. Ipstenu (Mika Epstein)

    As for why this hasn't been fixed in core, I have no idea except the depth and breadth of this type of change will require extensive testing to make sure all paths are re-written.

    Psychotic levels. Given that we have to NOT break everyone using it today, AND all plugins that rightly assume this is how it works... *shudder*

    Also Bob, using the BACKTICK character wraps code tags, so please don't use it flagrantly like you did in your code snippet ;)

    Posted: 6 years ago #
  10. haizdesign

    Your solution is perfect and just what I needed. Good work - thanks.

    Posted: 6 years ago #

RSS feed for this topic

Reply »

You must log in to post.

  • Rating

    223 Votes
  • Status

    Duplicate of another idea