Arrow


Automatically generate your blog URL in WordPress posts Share on Facebook

When writing WordPress blog posts, there are two types of internal URLs that I use often: URLs for images and files that I’ve uploaded to the site, and URLs to other posts. Both types of URLs are vulnerable to changes in my blog setup or blog URL. For example, consider an image at:

http://www.theblog.ca/wp-content/uploads/2008/05/documentation_icon_50px.png

This image URL has to be hard-coded into my post. If I change my site URL from theblog.ca to peterscrazyblog.com, that image URL is no longer valid. To fix this, I can add a redirect rule. However, that means that I must forever keep theblog.ca and that the generated URL still points to theblog.ca. Alternatively, I could code a relative URL (such as /wp-content/uploads…) that bypasses the domain altogether, but that breaks the images on sites that syndicate my content (such as Planet WordPress) and I’m still in trouble if I change the path to the folder that stores all the uploaded files and images.

Similarly, if I link to another post on my blog, that URL could change if I change my site URL or if I change my permalink structure.

The solution that makes sure my links remain valid and that they follow any changes I make to my site URL is to use what is called “shortcodes” in WordPress. (A shortcode is a piece of code that you can insert into a post that will be replaced with something else when the post is rendered.)

I know that my site URL and the path to the uploads directory are defined within the WordPress admin panel (under the Settings > General and Settings > Miscellaneous menus). They should only need to be defined once and never typed again. Therefore, I’ve created a plugin (compatible with WordPress 2.6 and higher) that enables you to use shortcodes to pull the already-defined site URL and uploads directory whenever you need to generate URLs that are within your blog.

Using blog URL shortcodes

Once the plugin is installed, the two shortcodes available when you’re writing a post are [blogurl] and [posturl].

On my site:

  • [blogurl] will generate http://www.theblog.ca/
  • [blogurl noslash] will generate http://www.theblog.ca
  • [blogurl uploads] will generate http://www.theblog.ca/wp-content/uploads/
  • [blogurl uploads noslash] will generate http://www.theblog.ca/wp-content/uploads
  • [posturl id=375] will generate http://www.theblog.ca/blog-url-shortcodes
  • <a href="[posturl id=360]">recent post about recycling</a> will generate recent post about recycling

Download Peter’s Blog URL Shortcodes

Arrow

16 Responses to “Automatically generate your blog URL in WordPress posts”



  1. Jim says:

    Hey, Peter, have you considered adding your plugins to the official WordPress.org plugin repository? I've pretty much taken to not downloading any plugins that aren't on their site, simply from the perspective of getting the updates as they come available. I do like the idea of this plugin, though, as there are times I've wanted a quick and easy way to insert my link.



  2. Peter says:

    Thanks for the suggestion, Jim. I hope to have it on wordpress.org soon. You can see the other plugins (not a comprehensive list, sadly) I've added here.

    Update: this plugin has now been added to: http://wordpress.org/extend/plugins/peters-blog-url-shortcodes/



  3. nancy aka money coach says:

    Hey Peter - that raises a question for me. The plugin above suggests duplicate content (or is it truly just duplicate images?) which is something I've wondered about - I'd like to post my blog entries on my http://www.yourmoneybydesign.com site as well. However, I've heard google downgrades you for doing that. Do you know anything about that? Awesome plugin, btw. I'm going to tweet about it.



  4. nancy aka money coach says:

    oops - pardon - I think I misread the first paragraph. You're simply referring to changing the existing blog's url, not duplicate content.



  5. Peter says:

    Hey Nancy, no worries :) Regarding the duplicate content issue, I haven't had to deal with it, but if you're just double posting, from what I know you're just forcing Google to choose which post to display in their results. It might only show the post from one of the sites — not a huge problem if it comes to that.



  6. Steve Bossman says:

    Thank you all for you suggestions will give it a go!



  7. Daniel N. Lang says:

    Thanks, Peter. I've been looking for such a plugin a long time ago already, but have so far never found one. My skills are too limited to set it into practice, otherwise I would have tried it myself. Do you have any reports about conflicts with other plugins? I'm thinking about giving it a try, too.



  8. Peter says:

    Hi Daniel, so far I have not received any reports of conflicts with other plugins. If there are any conflicts, I will certainly look into addressing them.



  9. Mario says:

    Your plug-in returns the WordPress URL and not the blog URL. I have WordPress in a subfolder called WordPress. My blog url is http://site.com and my WordPress URL is http://site.com/wordpress. The latter is what your plug-in returns. For an update, it would be nice, if [blogurl] would return what I have entered under "Blog Address (URL)" in the settings. Otherwise it is rather irritating. Another [wordpressurl] tag would then be nice in addition to [blogurl].



  10. Peter says:

    Thanks for pointing that out, Mario. I'll add that for a future release. In the meantime, if anybody needs that URL distinction, you can follow the code in my plugin and use get_option('home') for that value instead of get_option('siteurl').



  11. Austin @ NicheWeapons says:

    Brilliant, thank you Peter - was just looking into shortcodes to do exactly this :)



  12. Greg says:

    I desperately want this plugin to work, but I'm having some issues with it. I'm running WP 2.7.1 with this plugin installed, yet everytime I try to use the shortcode, it doesn't convert the tag to the URL.. I just see "[blogurl]" in the middle of my page. Does the plugin definitely work with 2.7.1, and I'm just doing something stupid?

    Reply from Peter: Yes, it is confirmed to be working with 2.7.1 with and without the visual editor enabled. Perhaps there is a conflicting plugin or somehow it isn’t actually activated?



  13. Jen says:

    Is there a way to get this to work in custom fields?

    Reply from Peter: There may be a way to parse custom fields the same way as post / page bodies, but off the top of my head I do not know how.



  14. Don says:

    How would I add a token for [admin_email]?
    I tried but failed to hack it. I looks like any option for 'get_bloginfo' should work…I'm not good enough to figure it out I guess…any help? I'd like to just add this to what is there already…this plugin is so cool!

    Reply from Peter: It would look something like:

    function adminEmailFunc() {
    return get_bloginfo( ‘admin_email’ );
    }

    add_shortcode(’admin_email’, ‘adminEmailFunc’);



  15. Don says:

    Unbelievable! A free plugin and you help me hack it for free. You are extraordinary, Sir!



  16. Eric says:

    Peter, can you supply me with the code I would be able to use for your plugin to insert a short code for the post title?

    Thanks,
    Eric

    Reply from Peter: The example two posts above should give you enough of a framework. You have to search the WordPress documentation to see how to access the post title generally. Due to the number of requests that I get, the volunteer time I can dedicate is limited.

Speak your mind

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word