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 to 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.

How we used Advanced Custom Fields in Redesign

In my recent post about the plugins we used in the redesign of Datafeedr I mentioned I would go over how we used them.

The first one is Advanced Custom Fields. While you can do everything that this plugin does without using it, ACF sure helps to clean up the ‘add post’ interface as well as makes adding data to the built-in custom fields much easier and faster.

For example, when adding a new feature to our Features page, we needed fields to insert screenshots (image + description) and video IDs (IDs + description).

For the feature “Shortcodes“, we added 2 screenshots as well as 2 videos. Here’s what that looks like on the ‘add post page’:


Now it’s easy and understandable what type of data goes into each field.

To make the process of adding multiple screenshots and videos to a single post easier, we are using the Add-On Repeater Field to make life even easier. It’s not necessary but it’s cheap and worth it.

We used ACF for nearly every custom post type required for the new site. We have fields to add javascript to some pages but not others, fields for screenshots and videos and even fields to remove menus from specific pages of the site.

They also provide a much better alternative to get_post_meta() and that is get_field().

If you are the type to use the built-in custom fields in your WordPress Posts or Pages or custom post types, I would highly recommend checking out ACF.

New Ads Plugin – Beta Testers Wanted

We’ve rewritten our old Random Ads / Rotating Ads plugin. Those old plugins can be found here:

The new plugin has the following features:

– set impression limit (ad doesn’t get displayed after X number of impressions)

– set start date (date ad should not appear before)

– set end date (date ad should not appear after)

– has shortcode for use in posts, page and widgets

– has PHP code for use in template files

– works with caching plugins (tested with W3TC Enhanced Page Cache). Impressions are still counted and ads are ordered randomly even if you have caching enabled. (Doesn’t work if your site’s visitor has JavaScript disabled in their browser.)

– Add any type of ad.

– Set up groups for your ads to control where they display.

– Turn ON/OFF impression counting when admins view pages.

So far, no documentation. Use at your own risk. Hopefully set up is straightforward but videos and docs to follow.

Thanks for testing!

UPDATE 2013-03-18 11:32:36

We’ve officially released the plugin. Read more here:

5 Dynamically add link to WordPress menu based on user’s logged in status

In the recent redesign I wanted to modify the main menu (the horizontal menu that appears at the top of every page) to show a different menu item at the end of the menu based on the user’s logged in status.

To show you what I mean, here’s what the menu is supposed to look like when not logged in:


And here’s what the menu was supposed to look like when a user was logged in:


Due to some issues with W3 Total Cache and the fact that our logged in users were not logged in through WordPress (rather they are logged in through our Amember software), I was unable to get what I wanted while still caching pages. However, if you are simply relying on WordPress’s login system (and you probably are), this method will work. As a side note, fragment caching did not help solve the issue.

First, you need to make sure you have a menu configured under Appearance > Menus and that your menu is set to Primary Navigation.


Next is just a matter of adding a little code to your theme’s functions.php file. Here’s a working example for the Twenty Twelve theme (be sure to exclude the opening php tags:

After you add that to your theme’s functions.php file and refresh your page, you should notice a new link appended to your Primary Navigation menu and that the link dynamically changes based on the user’s logged in status.

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.