"", // default style is example.com/123 "redirect" => 302, // temporary redirect by default "pages_enabled" => "", // pages disabled by default "twitter_tools_fix" => "", // disabled by deafult "askapache_google_404" => "", "tweet_this_fix" => "", "html_shortlink_rel" => "", "http_shortlink_rel" => "", "rel_canonical" => "", "rev_canonical" => "", ); foreach($defaults as $key => $default_value) $options[$key] = (!isset($options[$key])) ? $default_value : $options[$key]; update_option("twitter_friendly_links", $options); } function twitter_friendly_links_relations() { $options = get_option("twitter_friendly_links"); $pages_enabled = ($options["pages_enabled"] == "checked") ? true : false; if (is_single() || ($pages_enabled && is_page())) { $options = get_option("twitter_friendly_links"); $html_shortlink_rel = ($options["html_shortlink_rel"] == "checked") ? true : false; $rel_canonical = ($options["rel_canonical"] == "checked") ? true : false; $rev_canonical = ($options["rev_canonical"] == "checked") ? true : false; global $post; $post_id = $post->ID; $short_url = twitter_link($post_id); $permalink = get_permalink($post_id); if ($html_shortlink_rel) echo "\n"; if ($rev_canonical) echo "\n"; if ($rel_canonical) echo "\n"; } } function twitter_friendly_links() { $options = get_option("twitter_friendly_links"); $style = $options["style"]; $redirect = $options["redirect"]; $pages_enabled = ($options["pages_enabled"] == "checked") ? true : false; $uri = $_SERVER["REQUEST_URI"]; $home = get_option("home"); $home = str_replace("http://".$_SERVER["SERVER_NAME"], "", $home); $uri = str_replace($home, "", $uri); if (ereg("^/{$style}([0-9]+)/?$", $uri, $regs)) { // Fix for the AskApache Google 404 plugin $askapache_google_404 = ($options["askapache_google_404"] == "checked") ? true : false; if ($askapache_google_404) { global $AskApacheGoogle404; remove_action("template_redirect", array($AskApacheGoogle404, 'template_redirect')); } $post_id = $regs[1]; $post = new WP_Query("p=$post_id"); if ($post->have_posts()) { $post->the_post(); if ($redirect == 301) header("HTTP/1.1 301 Moved Permanently"); elseif ($redirect == 302) header("HTTP/1.1 302 Found"); header("Location: ".get_permalink()); } elseif ($pages_enabled) { $post = new WP_Query("page_id=$post_id"); // Search thru pages if ($post->have_posts()) { $post->the_post(); if ($redirect == 301) header("HTTP/1.1 301 Moved Permanently"); elseif ($redirect == 302) header("HTTP/1.1 302 Found"); header("Location: ".get_permalink()); } } } else { global $post; // Link relations $http_shortlink_rel = ($options["http_shortlink_rel"] == "checked") ? true : false; if ($http_shortlink_rel) if (is_single() || ($pages_enabled && is_page())) header("Link: <" . twitter_link($post_id) . ">; rel=shortlink"); } } function twitter_friendly_links_admin_scripts() { $count_posts = wp_count_posts(); $total_posts = $count_posts->publish; $count_pages = wp_count_posts('page'); $total_pages = $count_pages->publish; $plugin_url = trailingslashit(get_bloginfo('wpurl')).PLUGINDIR.'/'. dirname(plugin_basename(__FILE__)); wp_enqueue_script('twitterfriendly_navsettings', $plugin_url.'/js/nav_settings.js', array('jquery')); wp_localize_script('twitterfriendly_navsettings', 'TwitterFriendly', array( "plugin_url" => $plugin_url, "total_posts" => $total_posts, "total_pages" => $total_pages, )); } function twitter_friendly_links_menu() { $twitter_friendly_admin = add_options_page('Twitter Friendly Links', 'Twitter Friendly Links', 8, __FILE__, 'twitter_friendly_links_options'); add_action("admin_print_scripts-$twitter_friendly_admin", "twitter_friendly_links_admin_scripts"); } function twitter_friendly_links_options() { $options = get_option("twitter_friendly_links"); if (isset($_POST["twitter-friendly-links-submit"])) { $options["style"] = $_POST["style"]; $options["redirect"] = $_POST["redirect"]; $options["pages_enabled"] = $_POST["pages_enabled"]; $options["twitter_tools_fix"] = $_POST["twitter_tools_fix"]; $options["askapache_google_404"] = $_POST["askapache_google_404"]; $options["tweet_this_fix"] = $_POST["tweet_this_fix"]; $options["html_shortlink_rel"] = $_POST["html_shortlink_rel"]; $options["http_shortlink_rel"] = $_POST["http_shortlink_rel"]; $options["rel_canonical"] = $_POST["rel_canonical"]; $options["rev_canonical"] = $_POST["rev_canonical"]; update_option("twitter_friendly_links", $options); } $style = $options["style"]; $redirect = $options["redirect"]; $pages_enabled = $options["pages_enabled"]; $twitter_tools_fix = $options["twitter_tools_fix"]; $askapache_google_404 = $options["askapache_google_404"]; $tweet_this_fix = $options["tweet_this_fix"]; $html_shortlink_rel = $options["html_shortlink_rel"]; $http_shortlink_rel = $options["http_shortlink_rel"]; $rel_canonical = $options["rel_canonical"]; $rev_canonical = $options["rev_canonical"]; $selected[$redirect] = " selected=\"selected\""; ?>

Twitter Friendly Links

General Settings

Make sure you get this right from the first time because changing this afterward will affect all your previous twitter friendly links and you're most likely to get 404 error messages on your old links. For more information please visit Twitter Friendly Links. Oh and make sure you save changes before copying any links from the table below.

/123
302 by default
id="pages_enabled" name="pages_enabled"/> The style for pages will be the same as for posts

Link Relations

Search engines and URL shorteners. Bunch of thoughts on linking relations, so here are the main options. A little note about Rev Canonical - it may hurt the web, so use with caution.

id="html_shortlink_rel" name="html_shortlink_rel" /> Adds a link rel="shortlink" to the head section of your posts and (if enabled) pages. Specification.
id="http_shortlink_rel" name="http_shortlink_rel" /> Passes a link rel="shortlink" along with the HTTP responses of your posts and (if enabled) pages. Specification.
id="rel_canonical" name="rel_canonical" /> Adds a link rel="canonical" href="permalink" to your HTML head in posts and (if enabled) pages.
id="rev_canonical" name="rev_canonical" /> Adds a link rev="canonical" href="shortlink" to your HTML head in posts and (if enabled) pages.

Compatibility

If you use any of the plugins listed below and you are experiencing problems with short linking, enable the fixes. If there's no fix for a plugin you're using you may request it on the Twitter Friendly Links page.

id="twitter_tools_fix" name="twitter_tools_fix"/> Linking fix for the Twitter Tools plugin. Described here
id="tweet_this_fix" name="tweet_this_fix"/> Linking fix for the Tweet This plugin.
id="askapache_google_404" name="askapache_google_404"/> Fix for the AskApache Google 404 plugin.

List of short links

publish; $count_pages = wp_count_posts('page'); $total_pages = $count_pages->publish; ?>
/ Pages'; else echo 'Pages (disabled)'; ?>
Displaying 1-15 of
have_posts()) { $query->the_post(); $friendly_link = twitter_link($query->post->ID); $i++; ?>
  Title Friendly Link
  Title Friendly Link
.
' . "\n"; echo '
' . "\n"; echo '

' . "Twitter Friendly" . "

"; echo '
'; twitter_friendly_links_inner_box(); echo "
\n"; } function twitter_friendly_links_inner_box($post) { if ($post->post_status != "publish") { echo "

Please publish to get Twitter links.

"; return; } $post_id = $post->ID; $friendly_link = twitter_link($post_id); $title_length = utf8_strlen($post->post_title); $link_length = utf8_strlen($friendly_link); $overall_length = $title_length + $link_length; $excess = 140 - $overall_length; if ($excess < 0) { $excess *= -1; $title = mb_substr($post->post_title,0,$title_length-($excess+4),'UTF-8') . "... "; } else { $title = $post->post_title . " "; } echo "

Friendly link: {$friendly_link}

"; echo "

Tweet: {$title}{$friendly_link}

"; echo "

Tweet this »

"; } if (!function_exists("utf8_strlen")) { function utf8_strlen($s) { $c = strlen($s); $l = 0; for ($i = 0; $i < $c; ++$i) if ((ord($s[$i]) & 0xC0) != 0x80) ++$l; return $l; } } function twitter_link($id = 0) { $options = get_option("twitter_friendly_links"); $style = $options["style"]; if ($id == 0) { global $post; $post_id = $post->ID; } else $post_id = $id; $friendly_link = get_option("home") . "/" . $style . $post_id; return $friendly_link; } function permalink_to_twitter_link($permalink) { $options = get_option("twitter_friendly_links"); $style = $options["style"]; $post_id = url_to_postid($permalink); $friendly_link = get_option("home") . "/" . $style . $post_id; return $friendly_link; } function tweet_this_fix($content) { $twitter_link = twitter_link(); $content = preg_replace("/href=\\\"http:\/\/twitter.com\/home\/\?status=([^\\\"]+)\\\"/", "href=\"http://twitter.com/home/?status=" . urlencode(get_the_title() . " " . $twitter_link) . "\"", $content); return $content; }