Monthly Archives: March 2013

4 Create your own WordPress shortcode for Permalinks

I cringe every time I see a link in a blog post that is an absolute URL to other content on the same site. Here’s an example of what I mean:

In that example post you see a link to another page in my blog.

But what happens when you edit the slug of the post? The link will break.

Or what happens if you move your blog from /blog to /news? The link will break.

Or even worse, what happens if you change the domain where the content is hosted from http://www.mysite.com to http://blog.mysite.com? The link will break.

That’s why I cringe every time I see a link with an absolute URL to another page on the same site.

What’s the solution? Shortcodes.

Here’s an example of how to programmatically add links to your posts without being susceptible to the problems I mentioned above. The following code can be added to your theme’s functions.php file or to a custom plugin you might have.

After you add that code to your site, here’s how you can use it when writing a blog post or page.

To get the link to another post on your site all you need is the ID of the post you want to link to. Then in your post you would do something like this:

Now my link’s URL will be generated from the database and no matter how much we edit the other post’s slug or path, the correct URL will always appear in this link.

What if you want to avoid creating the <a href="">...</a> HTML code? That’s easy. Just do this:

Now when the post is rendered, the <a> tag will automatically be created and the title of the post will show up.

If you want to control the title of the link, you can do this:

Let me provide one last example to clear up any confusion… Let’s say you wrote a post on your blog. The post you wrote is titled Hello, World!, it has a slug of hello-world and the post has the ID of 33.

Here’s what the shortcode would look like in your posts and what would be returned:

Hope this helps future-proof the links in your blog posts.

2 Plugins are good for you, but only in moderation

In the recent redesign of Datafeedr we choose 9 plugins (including 1 custom plugin) to build the new site. I don’t like to build sites using too many plugins. The more plugins that you activate the slower your site will potentially run. Also, the more plugins you have the more maintenance and updates you have to do. However the biggest reason I don’t install too many plugins is out of fear that those plugins won’t survive the next (minor or major) release of WordPress or will be abandoned/neglected by the plugin authors. I don’t hold that against the plugin developers. The plugins are free. Their time is expensive. Things change. Interests shift. Life happens.

I wanted to share the 9 plugins we used to build Datafeedr:

Over the next few weeks I’ll post about some of these plugins, what I like about them and in some cases how I tweaked them to suite our needs.