Image links in Posts and pages
-
Currently when you add an image to a post or page, the image element’s source attribute value is saved as an absolute link. This creates a problem when you move the site and database to a different server, for example moving it from a development server to the client’s remote server. For those who know to edit the image’s link, there should be no problem migrating the site. Why doesn’t WordPress give us the option of creating absolute or relative links to images?
-
Quite simply because that’s not how WordPress currently works. Enabling the usage of relative links (that behave in a predicable manner) would involve some major core rewriting that just has happened yet. Perhaps because of issues with backwards compatibility.
In the meantime, all we can suggest is using a search & replace plugin.
I appreciate your very quick reply. If I can edit the image to use a relative link, I don’t see why the Admin area can’t offer that option when a new post is created. I don’t see how doing so would affect backwards compatibility. Can this suggestion be forwarded for considered to the core developers?
If I can edit the image to use a relative link, I don’t see why the Admin area can’t offer that option
The key phrase in my answer above was “that behave in a predicable manner”. Using relative urls in WP sometimes works but not always. As for the suggestion, it’s one that some of the core devs have posed many times but, so far, it’s not been possible.
I always use relative links for my client’s sites, and you will find that they are recommended. The only time relative links don’t work is when the WordPress installation is not installed in the web root, and then is later migrated to the web root of a new domain or the same domain. So mysite.com/myblog would not allow relative links to work. And a simple warning message could tell the Admin to only use the absolute link in such a case. Or the Settings admin page could ask whether WordPress is sitting at the web root level or not.
I think it is possible, if it is implemented in the best way that covers the two possibilities mentioned above.
Links outside the site’s domain could be recognized as such and would be left as absolute links.
What other condition(s) would cause relative links to not work in WP?
It depends what permalink structure you are using and whether you are linking to a Page or Post.
Relative URLs are better to use in traditional (non CMS) sites. They let you pick up and move things.
In the case of a CMS, where your data is stored in a database, it becomes less effective in the long run.
What if I want to move my wp-content folder? What if I move my whole install up a level? The search/replace is easier, the more ‘exact’ you can make it. Searching for domain.com/wp-content/uploads/ is a lot easier and reliable than
<img src="/"
and hoping I don’t have anything goofy in there.The only time relative links don’t work is when the WordPress installation is not installed in the web root, and then is later migrated to the web root of a new domain or the same domain.
Or in Multisite. Or putting WP in it’s own folder but running it from web root.
That’s actually a very common installation method.
Before I continue, I want to thank you both for your replies.
@esmi, please explain how a link to an image can be different whether you are linking to a Page or Post.The image element needs a source file and a path to get there.
@ipstenu, I build CMS sites, and always found relative links to be better. Searching for domain.com/wp-content/uploads/ is not easier or more reliable when you start moving things around. When I develop a site I do so knowing where the site will be sitting on my client’s server, and the site structure need not be changed. Why not offer my suggestion for sites built and migrated where such complexities are not going to be an issue. A multi-site installation would be a problem, but that could be addressed in the admin set up which would use absolute links, and we would use search and replace. A plugin can always be a problem because of security, bad code, and WP version compatibility issues. Yes, I could write my own script to do the search and replace and be done with it. But an admin option for normal, non-complex WP usage would be very helpful for most users.
please explain how a link to an image can be different whether you are linking to a Page or Post.The image element needs a source file and a path to get there.
If you are using
/%postname%/
as your custom permalink structure and then switch to a date-based structure, you may start to have problems with relative urls that are intended to point to image attachment pages. Additionally, not everyone use wp-content/uploads and people/sites do change over time – causing relative urls to break.Decisions, not options, is a mantra you’ll hear around here.
When I develop a site I do so knowing where the site will be sitting on my client’s server, and the site structure need not be changed.
You do. We don’t. We can’t. There are over a million WP users today. I would put dollar down that no two have exactly the same setup. And because of that, the best way to support all of them is the way that’s been done.
Why not offer my suggestion for sites built and migrated where such complexities are not going to be an issue.
Because the users don’t know they’re the site that will have an issue.
Searching for domain.com/wp-content/uploads/ is not easier or more reliable when you start moving things around.
Sure it is, because of how WordPress stores your uploaded images. They are always in ‘a’ location. Single. It’s normally
/wp-content/uploads/
, but yes, you can move it. When you move it, say to/sneakycontent/myuploads/
, then you search/replace any extant posts, fix those images, and all your new ones go to/sneakycontent/myuploads/
– Everything’s fixed.A multi-site installation would be a problem, but that could be addressed in the admin set up which would use absolute links, and we would use search and replace.
That code would be way more problematic than the shenanigans to make relative image paths optional, without breaking the myraid types of installations.
Basically you’re the minority with your setup. Sorry :/
@esmi, I’m not sure how if you switch to a date-based structure, absolute links will not present the same problem as relative links.
@ipstenu, when users move a site and find they have a problem, they have to check the documentation, forums and discussion groups to find the search/replace solution. You have tons of documentation for WP, why not just add that simple warning, the nature of the possible problem, and what to do if it arises. I’ll bet the majority of users want to, and do, keep it simple, and more so with experience.
Why not make it easier for some, who may in fact be the majority, instead of putting everybody in the same “distressed” boat?
Thank you for your explanations! I still think it’s better to help those who will benefit from the option, rather than let everyone suffer. And with the option and its documentation, others may understand the problem better, and avoid it by deciding to plan well and keep things simple from the start when developing their WP site.
From now on when developing locally, I will edit the image link before saving the post. This will help me migrate later. And I will know where and how to fix a problem if it arises.
why not just add that simple warning, the nature of the possible problem, and what to do if it arises
Heh. Go on and search for how many people change their home and site URL and have no idea what happened to their blog. Or those who see warnings in Multisite about ‘You don’t seem to have wildcard subdomains setup…’ and are then stymied as to why they don’t work. Honestly, I wish a warning would work, but people just click away and scream at us later.
I still think it’s better to help those who will benefit from the option, rather than let everyone suffer.
And that is why you should use a plugin for this.
A case in point: https://www.ads-software.com/support/topic/cant-log-into-my-wordpress-cgi-sysindexhtml
I’ve answered so many of these in a day that I’ve asked for the core devs to consider adding another step to site url changes
I still think it’s better to help those who will benefit from the option, rather than let everyone suffer.
And that is why you should use a plugin for this.
@ipstenu, I am not sure I understand how your comment responds to my suggestion. Never the less, I’ll try to prevent the problem in future.
@esmi, I don’t how you find the time to respond to so many people.
I think we are done, unless you good folks have anything else to add.
Thank you.
I have a system… </mystery mode> ??
I do appreciate your frustration over this but, as Ipstenu has said, WP is trying to be many things to many people. As soon as any app starts doing that, it will have its own peculiarities and “features” that can seem pointless to those with “edge case” sites. I know the topic of relative urls crops up regularly on the wp-hackers list and I’m sure it does in discussions elsewhere, so you’re not alone in wanting it. It’s just from a practical perspective, it ain’t so easy.
Thank you both.
esmi, publish and profit from your “</mystery mode> system.
Hope you will both be there to help on the forum thread of my next question/feature request.
- The topic ‘Image links in Posts and pages’ is closed to new replies.