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:

  1. 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.
  2. 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.