Bitly powered short links integration to Textpattern CMS. Automatically generates short link for an article when it is published, or its URL is updated.
Using Composer:
$ composer require rah/rah_bitly
Or download an installer package.
To start using rah_bitly, you will first need to configure your Bitly access token and a field you want to use to store the shortened links. You can find the options in Textpattern admin at the page Admin / Preferences and from there Bitly Integration section. Included options are:
- Bitly access token is your generic access token. You can generate your own access token in your Bitly account API settings.
- Store in a field is the field that will be used to store the shortened links. This supports article custom fields.
Bitly links are generated for an article when one of the three conditions is met:
- When the article is published.
- When already published article’s URL title is modified.
- When the custom field used to store the link is emptied.
All three conditions only apply while the article is in, or is saved with, live or sticky status. Short links won’t be generated for unpublished articles.
All generated links will follow your Bitly account’s preferences, including your custom domain or preferred domain.
If requesting a short link from Bitly fails due to connection error, or because Bitly’s servers are temporarily unreachable, a new request is sent when the article is saved again. You will notice if the request has been successful when there is a shortened link in the custom field and error message is shown in the editor.
The link that the plugin will shorten is the article’s permanent link, the real location of the individual article page that Textpattern responds to. It’s the same location as where the View link points on the Write admin page.
Displaying Bitly links on your page templates and articles is easy and customizable due to the fact that a custom field is used to store the shortened links.
<txp:if_custom_field name="short_url">
<a href="<txp:custom_field name="short_url" />">
<txp:custom_field name="short_url" />
</a>
<txp:else />
<p>No short link.</p>
</txp:if_custom_field>
The above snippet would display shortened Bitly link when placed to article context. If article doesn’t have a short link, No short link.
message is shown instead.
The plugin offers rah_bitly.permlink
callback event that can be used to customize the article permanent link that is sent to Bitly. An example observer registered to the event might look something like:
register_callback('abc_example', 'rah_bitly.permlink');
function abc_example($event, $step, &$data): void { if ($data['articleData']['Section'] === 'example') { $data['permlink'] .= '#bitly'; } }
The above would add #bitly
anchor to the sent URL, if the article is published to the section named example
.
- Added
rah_bitly.permlink
callback event that can be used to customize the article permlink that is sent to Bitly.
- Bitly API version 4 compatibility.
- Textpattern >= 4.7.0 compatibility.
- Now requires Textpattern >= 4.7.0 and PHP >= 7.2.0.
- Fixed: Undefined variable notices during article updates.
- Added: Displays preferences group on Textpattern 4.6-dev’s Preferences panel.
- Improved: Cleaned up the installer.
- Added: Textpattern 4.5.0 compatibility. Thank you, Robert Wetzlmayr.
- Added: Finnish translation.
- Fixed: A potential issue with uninstaller SQL statement’s wildcards.
- Changed: Textpacks aren’t embedded to the code, but uses installer.
- Removed: Unnecessary HTML escaping in one of the preference controls.
- Removed: Functionality added in v0.2 that allowed picking new custom fields from HTTP POST data. This is for future compatibility.
- Removed: Dropped support to file_get_contents() and allow_url_fopen. cURL is now required in favor of future compliance and move to oAuth.
- Changed: Now uses Textpattern’s
script_js()
to output JavaScript blocks. - Improved: Escape URLs returned by Bitly so that it can not break JavaScript string. This is to prevent potential JavaScript injections.
- Changed: Preference section’s heading now uses title case, and option labels sentence case.
- Changed: Now picks up new custom fields from POST data.
- Stef, Markus, thank you for suggestions.
- Initial release.