How can you stop WordPress from publishing static url’s to the database instead of using a dynamic url set from your website root? Often, when creating a test environment, WP developers will insert final content in the development server only to then realize the server address will change when published to the final, live site.
The url change is easily resolved by using the get_bloginfo(‘url’) variable throughout a theme, but content placed in the WordPress editor, such as links or images, will still link to the development server.
Here’s a quick fix that’ll remove the url from your posts, while replacing it with a root directory.
function thisismyurl_clean_static_url($content) {
return str_replace('href="'.get_bloginfo('url'), 'href="', $content );
return str_replace('src="'.get_bloginfo('url'), 'src="', $content );
}
add_filter('content_save_pre','thisismyurl_clean_static_url','99');
So, is the problem the visual editor, which seems to default to putting in the domain name for links? I usually do all the content editing using html, so I don’t seem to have any problems, as I never put the domain in. So, I never have a problem moving the site to a live site where the users might use the visual editor.
There is one thing I disagree with in your post; I think you are misusing the term “relative URL”. If you start with a slash, then it’s not really relative; a relative URL changes depending on where you start from, and never has a slash at the beginning. It would start with either “..” or a directory name relative to the current directory. I personally avoid them as much as possible, although it does seem common to use them in some places like the stylesheet (eg, “images/xx.jpg”, which is relative to the stylesheet directory).
Perhaps there is a convention in the WordPress world that is different than the technical definition, though. I’m still a little new at WordPress.
Marty, you’re correct on both points. The problem occurs when using the visual editor in WordPress, when adding a link to a post it will include the full path and store that information to the database automatically. The same holds true for images, the image is include as the full URL (ie http://thisismyurl.com/wp-content/ …) instead of resolving from the post itself.
On the second point, you’re correct as well. A relative path should resolve from the current URL and trace back from that post. If using a standard permalinks structure then, this post would include ../../wp-content/ or something similar however, since all WordPress pages are actually served from the root and permalinks are an apache construct, there isn’t really a URL path to follow, so all links need to start somewhere. I struggled to come up with another term than relative, but was stumped.
Thanks, Christopher – to be honest, at first I wasn’t aware of the visual editor’s habit of using full URLs, so in checking that out, I learned something to watch out for. I really don’t like the visual editor personally, but I’m pretty fluent in HTML
.
I also don’t really know of good terms, unless you use “partial” and “complete”; but I suspect most non-technical users probably don’t really know the finer points of the terminology, so I usually try to be overly descriptive when I tell them about it.
Thanks for the post, by the way. One question, though: does it work automatically? I might want to use it, as my last site got a few bogus URLs because I initially put it up on my demo site, and the user I was working with that added content ended up inserting a few before I told her about it; now I understand how it probably happened, so in the future, your function might help prevent this.
BTW, I came across your site on the WP-hackers list; I was curious to see if it was really the name of your domain. You’ll be glad to know it is.
-Marty Fried
Marty, the code will execute automatically when a user clicks to save a draft or publish (I believe but haven’t tested the Draft). The hook I’m using is a lifesaver, it’s useful for editing content after a user edits but before it reaches the database.
WP-hackers has been a lifesaver for me, it’s a fantastic mailing list. Anybody who’s trying to learn WordPress coding should be on there for sure (http://lists.automattic.com/mailman/listinfo/wp-hackers).
… why isn’t this a default feature in WordPress? This is genius.
You know, I have no idea. It something that I’ve run as a function for a long time and rarely think about.
Adding this code to functions.php crashes my site, giving this error:
Parse error: syntax error, unexpected T_STRING in blah/blah/functions.php
Why could this be? Dreameaver gives a warning message on the two return lines as well.
Thanks for the catch Dan. There was a . missing on two lines, I’ve corrected it in the code sample.
No worries. My motivation was a purely selfish one however – I’d very much like to use this code!
Thanks for getting back to me.
It’s all good Dan, that’s why I wrote it