API
The Pretty Link Plugin for WordPress now offers an API (as of Pretty Link Version 1.3.28).
If you’re a developer and you want to offer your users the ability to create short links on their own domain then this is the way to go. There are 2 ways that the Pretty Link API can be accessed:
- As a Local WordPress API — there are a set of Pretty Link functions that have been defined and are usable by other WordPress plugin developers.
- As a Remote XML-RPC API — there are a set of XML-RPC based functions that can be used by any other application that is Internet enabled. It doesn’t matter what language your App is written in, if it is Internet-based or a client app running on the desktop — you can now offer your users the ability to create short links on their own domains.
How Does it Work for WordPress Developers?
Once your plugin is installed on your user’s WordPress site, it has direct access to functions loaded by all other active plugins. I’ve created a few functions that you can use in your plugin to effectively create short links. Here’s a list of the Pretty Link functions you can use:
/** Returns the API Version as a string. */
prli_api_version()
/** Get a Pretty Link for a long, ugly URL.
* @return false if failure the full Pretty Link url if success
*/
prli_create_pretty_link( $target_url, $slug = '', $name = '', $description = '', $group_id = '', $show_prettybar = '', $ultra_cloak = '', $track_me = '', $nofollow = '', $redirect_type = '' )
/* Get all the pretty link groups in an array suitable for creating a select box. */
prli_get_all_groups()
/* Get all the pretty links in an array suitable for creating a select box. */
prli_get_all_links()
/* Gets a specific link from a slug and returns info about it in an array. */
prli_get_link($slug)
Here’s are some examples of using this in code. You’ve first got to check to see that pretty link is active then you can call these functions to your hearts content:
Creating a drop down of Pretty Link Groups
<?php
if(is_plugin_active('pretty-link/pretty-link.php'))
{
?>
<h3>Groups</h3>
<select>
<?php
$groups = prli_get_all_groups();
foreach($groups as $group)
echo '<option value="' . $group['id'] . '">' . $group['name'] . '</option><br/>';
?>
</select>
<?php
}
?>
Creating a drop down of Pretty Link Links
<?php
if(is_plugin_active('pretty-link/pretty-link.php'))
{
?>
<h3>Links</h3>
<select>
<?php
$groups = prli_get_all_links();
foreach($links as $link)
echo '<option value="' . $link['id'] . '">' . $link['name'] . '</option><br/>';
?>
</select>
<?php
}
?>
Creating a Pretty Link
<?php
if(is_plugin_active('pretty-link/pretty-link.php'))
{
if( $pretty_link = prli_get_pretty_link( 'http://blairwilliams.com/wishlist', '', 'My Cool Wishlist Link' ) )
echo $pretty_link;
else
{
foreach($prli_error_messages as $prli_error_message)
echo $prli_error_message . '<br/>';
}
}
?>
How Does it Work for Non-WordPress Developers?
Here’s a listing of the XML-RPC methods that are available for you to use and some examples of how to use them:
/** Returns the API Version as a string. */
string:prli.api_version( string:username, string:password )
/** Get a Pretty Link for a long, ugly URL. */
string:prli.create_pretty_link( string:username, string:password, string:target_url, string:slug = '', string:name = '', string:description = '', int:group_id = '', boolean:show_prettybar = '', boolean:ultra_cloak = '', boolean:track_me = '', boolean:nofollow = '', string:redirect_type = '' )
/* Get all the pretty link groups in an array suitable for creating a select box. */
array:prli.get_all_groups( string:username, string:password )
/* Get all the pretty links in an array suitable for creating a select box. */
array:prli.get_all_links( string:username, string:password )
/* Gets a specific link from a slug and returns info about it in an array. */
array:prli.get_link( string:username, string:password, string:slug )
… and here are the examples:
Accessing the Remote XML-RPC API using WordPress’s PHP XML-RPC library:
<?php
include(ABSPATH."/wp-includes/class-IXR.php");
$client = new IXR_Client('http://mydomain.com/xmlrpc.php');
$username = "admin";
$password = "bbllaahh";
if (!$client->query('prli.get_all_groups',$username,$password)) {
die('An error occurred - '.$client->getErrorCode().":".$client->getErrorMessage());
}
print_r($client->getResponse());
if (!$client->query('prli.get_all_links',$username,$password)) {
die('An error occurred - '.$client->getErrorCode().":".$client->getErrorMessage());
}
print_r($client->getResponse());
if (!$client->query('prli.get_link',$username,$password,'aweber')) {
die('An error occurred - '.$client->getErrorCode().":".$client->getErrorMessage());
}
print_r($client->getResponse());
if (!$client->query('prli.create_pretty_link',$username,$password,'http://cnn.com','','CNN')) {
die('An error occurred - '.$client->getErrorCode().":".$client->getErrorMessage());
}
print_r($client->getResponse());
if (!$client->query('prli.api_version',$username,$password)) {
die('An error occurred - '.$client->getErrorCode().":".$client->getErrorMessage());
}
echo $client->getResponse();
?>
I haven’t yet researched the syntax of accessing this library using XML-RPC clients in other languages but XML-RPC libraries exist for almost every language out there — so you’ll be able to access the API if you’re using Java, Ruby, ActionScript, Python, Perl or even .NET!
This API is brand new (version 1.0) so if you spot any issues or have suggestions for it, feel free to leave a comment on this page.












{ 2 trackbacks }
{ 18 comments… read them below or add one }
This is an amazing present to find in my feed reader
A huge thank you, Blair!
Great plugin mate. I have added it to my blog and have recommended the XML-RPC for another music site.
Awesome — let me know if you have any questions about using the API — it still has a very small amount of documentation — but I’m happy to help. Good Luck!
What a wonderful plug-in. The API is great as well, and will really allow me to add a lot of great functionality to my website. Thanks for the hard work!
-Bill
I think I found a type-o in one of the sample Creating a drop down of Pretty Link Links
The line:
$groups = prli_get_all_links();
Should read:
$links = prli_get_all_links();
Thanks for catching that … Just thought you’d like to know that I’ve updated the api and will be updating this page within the next few days. If you run into any issues let me know and I’ll help any way I can…
Is there a way to get all of the links from a particular category?
Not yet, that’s a great idea — I just have the basic CRUD operations right now. I’ll add a function for that and let you know when it’s complete. Look for it early this week.
Hmmm the code for “Creating a Pretty Link” doesn’t work at all using Wordpress 2.8.
Call to undefined function prli_get_pretty_link().
The plugin is active
There’s no function in the pretty link api called prli_get_pretty_link … Try using:
function prli_get_link_from_slug($slug)or
function prli_get_link($id)or
function prli_get_pretty_link_url($id)Let me know if this helps.
The API is an awesome feature, thx !
Do you see a possibility of prettylink (reg and pro) to be able to use remote API calls to a prettylink pro install or will this functionality only be available via a third party script?
Example what I am asking:
Posting an article on verylongdomain.com/interest-category/myarticleisthebest.htm
remote api call to shorturl.com to create a shorturl (shorturl.com/plp) and then posting to twitter (would be great if you can set both the initiating domain AND the shorturl PLP install to post to twitter).
Does this make sense?
Thanks for the nudge
… Sorry it took so long to respond. The answer is yes. I have a feature on the list that would allow users to create shortlinks on a completely separate install of Pretty Link. The first part of this feature will be just being able to create aliases to internal Pretty LInks — this is going to be critical going forward. The second part is tweeting from both locations … which will also be implemented sometime soon. This will only work for pro users though.
Hi
Could you please tell me how to use this plugin integrated with “Tweet This” plugin.
That requires the ApI in a format similar to this: “http://h3o.de/api/index.php?url=[LONGURL]”
The plugin options page says this: “The URL shortening service must allow HTTP GET requests, meaning that the long URL is passed to the API as a parameter in the URL itself. HTTP POST is not supported. The output of the API must be a plain-text short URL including the http prefix. The only valid variable is “[LONGURL]“.
Kindly help.
Thanks
S.K
The API is currently only XML-RPC which uses POSTs. The closest thing I have to this is the bookmarklet url but I’m not sure Tweet This could read the output from it (It’s HTML — not an API return). Since Tweet This is a WordPress plugin, the best thing it could do is access my API directly (no need for get or post — it’s a simple function call in PHP).
Also, you should know that the Pro upgrade of Pretty Link (Pretty Link Pro) does everything that tweet this does and more … and it will use your pretty links. It will let you auto-tweet your posts, display a tweet badge for users to view the number of tweets and retweet the post — it even aggregates the twitter conversations going on about your post and will display them under your post. You should check it out.
Thanks for your prompt reply.
I’ll definitely consider upgrading soon.
Regards
S.K
Hi Blair,
When you mean creating aliases to internal pretty links, what exactly are you referring to?
Do you foresee a Pretty Link Pro install being able to create shorturls for third party sites via API? Either by using regular Pretty Link or some other kind of plugin/function.
That would allow pretty link pro owners to start their own shorturl service like bit.ly but prettier.
Yeah — there are some people who are turned off by using their own domain name for URL shortening (usually when it’s gigantic) — I mean it’s great to be able to post links using your domain name, but when your domain name is > 30 characters long what do you do? I’m planning on implementing some options that will give users some options on what they can do to really shrink down their links.