Add Shortt URL Array to WordPress Option Table

To cater for a few articles that are scheduled over the next few weeks, starting with the article that follows this one (in about an hour), I’ve written a quick WordPress function that will save links to an array stored in the WordPress options table . Access to the API with a valid API key is required before use.

The article scheduled an hour from now will render text Twitter links and, as part of that, we optionally provide to truncate the URL that’s provided. To ensure we don’t shorten the same URL over multiple pages we store the results in a single field using the WordPress options table .

In a previous article we showed you how to display a URL for every post and page under each article… and in that case we stored the result as an individual option.

We’ll be referencing this function in the future for inclusion with a number of other WordPress shortcodes and snippets.

The Code

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

Ensure that you set your correct API key. If you’re using another shortener of ours , set the $domain as required. If you’re a client and using your own shortener, use that instead. One API key only works on one domain.

How URLs are Stored

Data is stored in the wp_options table with the key of beliefmediashortturls.

The data is automatically serialised by WordPress.

More information is available from WordPress options table .

How we use it?

Every single link on this website is created with shortcode. For example, a snapshot of our post editor of the text above looks like this:

Note that we’ve wrapped our links in link shortcode. This means that we can control the behaviour of that link globally well outside the scope of traditional CSS. For example, we attach a favicon for the destination website, we automatically link external websites … and we can optionally truncate links with the attribute of"1" – meaning that we can track the success or failure of certain links within certain articles. We can optionally set an expiry on a link, render the click count alongside the link, or use the post ID for internal links to avoid permalink issues. The features just described will be published in a scheduled article in early January. We periodically and automatically update the link count on a page in our control panel to give us a snapshot of what’s performing and what is not.

The Twitter function that follows this article obviously includes the option to automatically truncate links.

Download the Code

Download the WP Function here

Shortt URL for this post:

Leave a Reply

Your email address will not be published. Required fields are marked *