Thanks to Joost de Valk, and his WordPress plugins on this one;
many issues we encountered were solved by looking at his great work.
Copy the following program to "wordpress-tiny-urls.php" within your "wp-content/plugins/" folder.
<?php
/*
Plugin Name: Tiny URLs
Plugin URI: http://www.seoegghead.com/software/wordpress-tiny-urls.seo
Description: Creates customizable shortened URLs for WordPress.
Author: SEO Egghead, Inc.
Version: 1.2 for WP 2.x
Author URI: http://www.seoegghead.com/
*/
add_option('Tiny_URL_Post_count',1);
add_option('Tiny_URL_Redirect', '301');
add_option('Tiny_URL_Strip', 'N');
add_option('Tiny_URL_Email', 'N');
add_option('Tiny_URL_email_address', get_option('admin_email'));
add_option('Tiny_URL_Show', 'N');
add_option('Tiny_URL_Credit', 'N');
add_option('Tiny_URL_Before', '<br />Tiny URL: <br />');
add_option('Tiny_URL_After', '<br />');
add_option('Tiny_URL_Conditionals', serialize(array()));
//modified from sociable plugin
function Tiny_URL_meta() {
global $post;
$tiny_url_off = false;
$tiny_url_offmeta = get_post_meta($post->ID,'Tiny URL',true);
if ($tiny_url_offmeta == "disabled") {
$tiny_url_off = true;
}
?>
<input type="checkbox" value="Y" name="tiny_url_off"
<?php if ($tiny_url_off) { echo 'checked="checked"'; } ?>/> Tiny URL disabled?
(This will remove any custom urls)
<?php
}
//modified from sociable plugin
function Tiny_URL_option() {
global $post;
$tiny_url_off = false;
$tiny_url_off_meta = get_post_meta($post->ID,'Tiny URL',true);
if ($tiny_url_off_meta == "disabled") {
$tiny_url_off = true;
}
if ( current_user_can('edit_posts')) { ?>
<fieldset id="tiny_url_option" class="dbx-box">
<h3 class="dbx-handle">tiny_url_</h3>
<div class="dbx-content">
<input type="checkbox" name="tiny_url_off"
<?php if ($tiny_url_off) { echo 'checked="checked"'; } ?>/>
Tiny URL disabled? (This will remove any custom urls)
</div>
</fieldset>
<?php
}
}
//modified from sociable plugin
function Tiny_URL_meta_box() {
// Check whether the 2.5 function add_meta_box exists,
//and if it doesn't use 2.3 functions.
if ( function_exists('add_meta_box') ) {
add_meta_box('tiny_url_','Tiny URL','tiny_url_meta','post');
add_meta_box('tiny_url_','Tiny URL','tiny_url_meta','page');
} else {
add_action('dbx_post_sidebar', 'tiny_url_option');
add_action('dbx_page_sidebar', 'tiny_url_option');
}
}
add_action('admin_menu', 'Tiny_URL_meta_box');
function save_Tiny_URL($post_id){
$tinyURL = get_post_meta($post_id, 'Tiny URL', true);
$post = get_post($post_id);
if(!$post->post_parent){
delete_post_meta($post_id, 'Tiny URL');
if ($_POST['tiny_url_off'] == 'Y'){
update_post_meta($post_id, 'Tiny URL', 'disabled');
}else{
if ($tinyURL == 'disabled' || $tinyURL == ''){
$count = get_option('Tiny_URL_Post_count');
update_option('Tiny_URL_Post_count', ++$count);
$tinyURL = '/' . base_convert($count, 10, 36) . '.tiny';
}
add_post_meta($post_id, 'Tiny URL', $tinyURL, true);
if($post->post_status == 'publish'){
tiny_URL_send_log_message($post_id);
}
}
}
}
add_action('save_post', 'save_Tiny_URL');
function tiny_URL_send_log_message($post_id){
global $wpdb;
$post_id_query = "
SELECT `meta_value`
FROM `$wpdb->postmeta`
WHERE meta_key = 'Tiny URL'
AND post_id = $post_id
ORDER BY `meta_id` DESC
LIMIT 1 ";
$tiny_url_suffix = $wpdb->get_var($post_id_query);
if(get_option('Tiny_URL_Email') == 'Y' && $tiny_url_suffix != 'disabled'){
$tiny_url = get_option('siteurl') . $wpdb->get_var($post_id_query);
$post_url = get_permalink($post_id);
$plugin_url = get_option('siteurl') .
'/wp-admin/options-general.php?page=wordpress-tiny-urls.php';
if(get_option('Tiny_URL_Strip') == 'Y'){
$tiny_url = preg_replace('#^http://(www\.)#', '', $tiny_url);
$options = 'We stripped off the leading www of your domain to make
it even shorter. ';
}
$options .= 'It will be redirected via ' . get_option('Tiny_URL_Redirect');
$message =<<<EndMessage
<table border="0" cellpadding="5">
<tr>
<td>
WordPress Tiny URLs has created a shortened URL of $post_url as $tiny_url.
$options.
</td>
</tr>
<tr><td><br />
<a href="$plugin_url">Click here</a>
to turn off these emails, change redirect method, etc.
</td></tr>
</table>
<br />
<div style="float:right; position:relative; top:-80px;">
<a
href="http://www.seoegghead.com/software/wordpress-tiny-urls.seo"
style="text-decoration:none;" target="_blank">
<img src=
"http://www.seoegghead.com/cms_pics/seoegghead_embedded_smaller_faded.png"
border="0" />
<br />
<small>Click here for plugin documentation.</small>
</a>
<br />
<small>Got Questions or Feedback?
<a style="text-decoration:none;" href=
"http://www.seoegghead.com/about/contact-us.seo?subject=
Tiny+URL+Feedback"
target="_blank">Click here.</a>
</small>
<br />
<small>By using this plugin you agree to
<a style="text-decoration:none;"
href="http://www.seoegghead.com/software/free-software-disclaimer.seo"
target="_blank">this simple disclaimer.
</a>
</small>
</div>
EndMessage;
$address = get_option('Tiny_URL_email_address');
$header = "Content-Type: text/html\r\n";
$header .= "From: " . $address . "\r\n";
$subject = 'Notification from WordPress Tiny URLs on '
. get_option('siteurl');
mail($address,$subject,$message, $header);
}
}
function redirect_tiny_URL_pages () {
if(is_404()){
global $wpdb;
$postmetaquery = "
SELECT `post_id`, `meta_value`
FROM `$wpdb->postmeta`
WHERE meta_key LIKE 'Tiny URL'
AND meta_value != 'disabled'";
$postarr = $wpdb->get_results($postmetaquery,ARRAY_A);
foreach($postarr as $arr){
if((get_bloginfo('wpurl') .$arr['meta_value']) ==
('http://' . $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]))
{
$url = get_permalink($arr['post_id']);
if(get_option('Tiny_URL_Redirect') == '301'){
header("HTTP/1.0 301 Moved Permanently");
}else{
header('HTTP/1.0 302 Found');
}
header ("Location: $url");
}
}
}
}
add_action('template_redirect', 'redirect_tiny_URL_pages');
//modified from sociable plugin
function tiny_url_display_hook($content=''){
$conditionals = unserialize(get_option('Tiny_URL_Conditionals'));
global $wp_query, $post;
$post_id = $wp_query->post->ID;
$tinyurl = get_post_meta($post_id, 'Tiny URL', true);
if (!$tinyurl or $tinyurl == 'disabled' or
(is_home() and !$conditionals['is_home']) or
(is_single() and !$conditionals['is_single']) or
(is_page() and !$conditionals['is_page']) or
(is_category() and !$conditionals['is_category']) or
(is_tag() and !$conditionals['is_tag']) or
(is_date() and !$conditionals['is_date']) or
(is_search() and !$conditionals['is_search'])
){}
else{
$tinyurl = get_bloginfo('wpurl') . $tinyurl;
if(get_option('Tiny_URL_Strip') == 'Y'){
$tinyurl = preg_replace('#^http://(www\.)#', '', $tinyurl);
}
$urllength = round(strlen($tinyurl) * 1.2);
$html =
'<!-- http://www.seoegghead.com/software/wordpress-tiny-url-generator.seo-->'
. get_option('Tiny_URL_Before')
. "<input type=\"text\" readonly size ='$urllength' value='$tinyurl'
onclick=\"this.select();\"></input>";
if(get_option('Tiny_URL_Credit') == 'Y' ){
$html .=
"<br />
<small >
powered by
<a href=\"http://www.seoegghead.com/software/wordpress-tiny-urls.seo\">
WP Tiny URLs</a>
</small>";
}
$html .= get_option("Tiny_URL_After") .
'<!--END http://www.seoegghead.com/software/wordpress-tiny-url-generator.seo-->';
$content .= $html;
}
return $content;
}
if(get_option('Tiny_URL_Show') == 'Y'){
add_filter('the_content', 'tiny_url_display_hook');
add_filter('the_excerpt', 'tiny_url_display_hook');
}
add_action('admin_menu', 'Tiny_URL_admin_menu');
function Tiny_URL_admin_menu() {
add_submenu_page('options-general.php', 'Tiny URLs', 'Tiny URLs',
8, __FILE__, 'Tiny_URL_submenu');
}
function Tiny_URL_submenu(){
if ($_REQUEST['save']) {
update_option('Tiny_URL_Redirect', $_REQUEST['redirect']);
update_option('Tiny_URL_Strip', $_REQUEST['strip']);
update_option('Tiny_URL_Email', $_REQUEST['email']);
update_option('Tiny_URL_Show', $_REQUEST['show']);
update_option('Tiny_URL_Credit', $_REQUEST['credit']);
update_option('Tiny_URL_Before', stripslashes($_REQUEST['before_text']) );
update_option('Tiny_URL_After', stripslashes($_REQUEST['after_text']));
update_option('Tiny_URL_Conditionals', serialize($_REQUEST['conditionals']));
echo '<br />
<div class="updated fade">
<p>Tiny URL settings updated.</p>
</div> ';
}
$conditionals = ($_REQUEST['conditionals']) ? $_REQUEST['conditionals'] :
unserialize(get_option('Tiny_URL_Conditionals'));
?>
<form name="tinyurl"
action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post" >
<div class="wrap">
<h2>Tiny URLs</h2>
<h3>Settings:</h3>
<table class="form-table" border="0">
<tr>
<th scope="row" valign="top" >
Redirect via:
</th>
<td>
<input type="radio" value="301" name="redirect"
<?php echo get_option('Tiny_URL_Redirect') == '301' ? "checked" : "" ?> > 301
<input type="radio" value="302" name="redirect"
<?php echo get_option('Tiny_URL_Redirect') == '302' ? "checked" : "" ?> > 302
</td>
</tr>
<th scope="row" valign="top" >
Strip Leading 'www':
</td>
<td>
<input type="radio" value="Y" name="strip"
<?php echo get_option('Tiny_URL_Strip') == 'Y' ? "checked" : "" ?> > Y
<input type="radio" value="N" name="strip"
<?php echo get_option('Tiny_URL_Strip') == 'N' ? "checked" : "" ?> > N
</td>
</tr>
<tr>
<th scope="row" valign="top" >
Send Email After URL Created:
</td>
<td>
<input type="radio" value="Y" name="email"
<?php echo get_option('Tiny_URL_Email') == 'Y' ? "checked" : "" ?> > Y
<input type="radio" value="N" name="email"
<?php echo get_option('Tiny_URL_Email') == 'N' ? "checked" : "" ?> > N
</td>
</tr>
<tr>
<th scope="row" valign="top" >
Show Tiny URL After Post:
</th>
<td>
<input type="radio" value="Y" name="show"
<?php echo get_option('Tiny_URL_Show') == 'Y' ? "checked" : "" ?> > Y
<input type="radio" value="N" name="show"
<?php echo get_option('Tiny_URL_Show') == 'N' ? "checked" : "" ?> > N
</td>
</tr>
<tr>
<th scope="row" valign="top" >
Show TinyURL Credit:
</th>
<td>
<input type="radio" value="Y" name="credit"
<?php echo get_option('Tiny_URL_Credit') == 'Y' ? "checked" : "" ?> > Y
<input type="radio" value="N" name="credit"
<?php echo get_option('Tiny_URL_Credit') == 'N' ? "checked" : "" ?> > N
</td>
</tr>
<tr>
<th scope="row" valign="top" >
HTML Before Link:
</th>
<td>
<input type="text" style="min-width:10em"
size="<?php echo strlen(get_option('Tiny_URL_Before'))?>"
value="<?php echo htmlspecialchars(get_option('Tiny_URL_Before'), ENT_QUOTES)?>"
name="before_text" >
</td>
</tr>
<tr>
<th scope="row" valign="top" >
HTML After Link:
</th>
<td>
<input type="text" style="min-width:10em"
size="<?php echo strlen(get_option('Tiny_URL_after'))?>"
value="<?php echo htmlspecialchars(get_option('Tiny_URL_After'), ENT_QUOTES)?>"
name="after_text" >
</td>
</tr>
<tr>
<th scope="row" valign="top" >
Placement:
</th>
<td>
<?php _e("Tiny URLs appear at the end of each blog post, but posts may show on many
different types of pages. Please select from the list below — ",
'tiny_url_');?><br/>
<br/>
<input type="checkbox" name="conditionals[is_home]"<?php echo ($conditionals['is_home']) ? ' checked="checked"' : ''; ?> /> <?php _e("Front page of the blog", 'tiny_url_'); ?><br/>
<input type="checkbox" name="conditionals[is_single]"<?php echo ($conditionals['is_single']) ? ' checked="checked"' : ''; ?> /> <?php _e("Individual blog posts", 'tiny_url_'); ?><br/>
<input type="checkbox" name="conditionals[is_page]"<?php echo ($conditionals['is_page']) ? ' checked="checked"' : ''; ?> /> <?php _e('Individual WordPress "Pages"', 'tiny_url_'); ?><br/>
<input type="checkbox" name="conditionals[is_category]"<?php echo ($conditionals['is_category']) ? ' checked="checked"' : ''; ?> /> <?php _e("Category archives", 'tiny_url_'); ?><br/>
<input type="checkbox" name="conditionals[is_tag]"<?php echo ($conditionals['is_tag']) ? ' checked="checked"' : ''; ?> /> <?php _e("Tag listings", 'tiny_url_'); ?><br/>
<input type="checkbox" name="conditionals[is_date]"<?php echo ($conditionals['is_date']) ? ' checked="checked"' : ''; ?> /> <?php _e("Date-based archives", 'tiny_url_'); ?><br/>
<input type="checkbox" name="conditionals[is_search]"<?php echo ($conditionals['is_search']) ? ' checked="checked"' : ''; ?> /> <?php _e("Search results", 'tiny_url_'); ?><br/>
</td>
</tr>
</table>
<br />
<span class="submit">
<input name="save" value="Save Changes" type="submit" />
</span>
</div>
<br /><br />
<?php
show_Tiny_URL_plugin_link();
}
function show_Tiny_URL_plugin_link(){
?>
<div style="float:right; position:relative; top:-60px;">
<a href="http://www.seoegghead.com/software/wordpress-tiny-urls.seo"
style="text-decoration:none;" target="_blank">
<?php
if(preg_match('#MSIE#', $_SERVER['HTTP_USER_AGENT']) == 0) {
?>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHgAAAAUCAMAAA
BxjAnBAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADBQTFRFz6Ol
opmRs21w48/P+Pb218bF9Orr6OTit7CpqVpdqaGZrmJlvYCD0ry769vc////wFM5SwAAAB
B0Uk5T////////////////////AOAjXRkAAAJLSURBVHjatFXtDiMhCERF3Q/R93/bGxDX
Xu5Pr2lJNk7pVHQYujS+FImov09Ng75VmM4zvU+V7xVu583vU7sVZo/CG7+kDRcL27qs4J
VWfJ+N/+GMzdi7cAQVhfm4PEJlcXjUkVf64CHxtogySnN8N64Ppn6erc10Gd3zERQn3DGN
ZIQGKq5LQ8ITnB+Y6073dK5oTBtDsxWUH3gmXGlF33nytFIFhfkKV68avSpOQDhLwhmk9t
6vECoKUEpJtHA8bwFWg5T7jHlifZSSsXnBORtwArWWZIEdREUBvYAK/1MJ4bC6aAhumRkQ
ResRLusW1oKmDJxBj1pMJ3OmCiET36Wdkadxon1XwNcc6+G76tTFfT+ptCWVkVAwGb7qFQ
5lQYRDm1JdRhTPy8Tz6EPNUvVsaqpoHbh5dqEVW+e5QC9KuY1KnK+nxTKfoCLg8rqTrmkW
xAZ/FfOjDxWhuxC2RuuI0oXcGTNldKfSGhuVVh8PXNz+iHS1Yj4Ubf+a/ZaquLgQKqd1w8
cIxSrGFHqR33Q4lbIHJIWsQeYn9fqBVWA3lauRBWNcb6ymOAootnO5jNregu/ipCfQDWIH
1c2wWk6Ze2oSP/CApx78Mh3McxXzj7cdLa0uo8qpV/KQsoes7Jk0Kh0rEvR98CjiMDMG3w
MKioFqhqWJo3qoWYtt9TyiD1mw8U7Psfjoj5m9X2Ob5b9fKp+9A1+KiU/zbwvn6ZqozpQH
958X3j6Ly2c2pj8vXB5/VLyCFi7j9zeWGQSvF/IP6ZMLjz8CDACmemOuUH7ZzQAAAABJRU
5ErkJggg==" />
<?php
}
?>
<br />
<small>Click here for plugin documentation.</small>
</a>
<br />
<small>Got Questions or Feedback?
<a style="text-decoration:none;"
href=
"http://www.seoegghead.com/about/contact-us.seo?subject=Tiny+URLs+Feedback"
target="_blank">
Click here.
</a>
</small>
<br />
<small>By using this plugin you agree to
<a style="text-decoration:none;"
href="http://www.seoegghead.com/software/free-software-disclaimer.seo"
target="_blank">
this simple disclaimer.
</a>
</small>
</div>
<?php
}