. */ add_action("init", "twitter_friendly_links_init", 9); function twitter_friendly_links_init() { add_action('template_redirect', 'twitter_friendly_links', 9); add_action("admin_menu", "twitter_friendly_links_menu"); add_action("admin_menu", "twitter_friendly_links_box"); $options = get_option("twitter_friendly_links"); // Fix for the Twitter Tools & Tweet This plugins $twitter_tools_fix = ($options["twitter_tools_fix"] == "checked") ? true : false; $tweet_this_fix = ($options["tweet_this_fix"] == "checked") ? true : false; $sociable_fix = ($options["sociable_fix"] == "checked") ? true : false; if ($twitter_tools_fix) add_filter("tweet_blog_post_url", "permalink_to_twitter_link", 10, 1); if ($tweet_this_fix) add_filter("the_content", "tfl_tweet_this_fix", 10); if ($sociable_fix) add_filter("sociable_link", "tfl_sociable_fix"); // Link relations options $html_shortlink_rel = ($options["html_shortlink_rel"] == "checked") ? true : false; $rel_canonical = ($options["rel_canonical"] == "checked") ? true : false; if ($html_shortlink_rel || $rel_canonical) add_action("wp_head", "twitter_friendly_links_relations"); // Notice about core changes if ($options["tfl_core_notice"] == 1 && !isset($_GET["tfl_hide"])) add_action("admin_notices", "tfl_admin_notices_core"); // Notify the administrator if permalinks are switched off $permalink_structure = (isset($_POST['permalink_structure'])) ? $_POST['permalink_structure'] : get_option("permalink_structure"); if ($permalink_structure == "") add_action("admin_notices", "tfl_admin_notices"); } function twitter_friendly_links_relations() { $options = get_option("twitter_friendly_links"); $posts_enabled = ($options["posts_enabled"] == "checked") ? true : false; $pages_enabled = ($options["pages_enabled"] == "checked") ? true : false; $attachments_enabled = ($options["attachments_enabled"] == "checked") ? true : false; if (($posts_enabled && is_single()) || ($pages_enabled && is_page()) || ($attachments_enabled && is_attachment())) { $options = get_option("twitter_friendly_links"); $html_shortlink_rel = ($options["html_shortlink_rel"] == "checked") ? true : false; $rel_canonical = ($options["rel_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 ($rel_canonical) echo "\n"; } } function twitter_friendly_links() { $options = get_option("twitter_friendly_links"); $style = $options["style"]; $format = $options["format"]; $redirect = $options["redirect"]; $posts_enabled = ($options["posts_enabled"] == "checked") ? true : false; $pages_enabled = ($options["pages_enabled"] == "checked") ? true : false; $attachments_enabled = ($options["attachments_enabled"] == "checked") ? true : false; $ga_tracking = (strlen($options["ga_tracking"]) > 1) ? "?".$options["ga_tracking"] : ""; $uri = $_SERVER["REQUEST_URI"]; $home = get_option("home"); $home = str_replace("http://".$_SERVER["SERVER_NAME"], "", $home); $uri = strtolower(str_replace($home, "", $uri)); if (ereg("^/{$style}([0-9a-z]+)/?$", $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]; if ($format == "base32") $post_id = tfl_base32($post_id, true); $posts = new WP_Query("p=$post_id&post_type=any"); if ($posts->have_posts()) { $posts->the_post(); $post = $posts->post; if (!$posts_enabled && $post->post_type == "post") return; if (!$pages_enabled && $post->post_type == "page") return; if (!$attachments_enabled && $post->post_type == "attachment") return; if ($redirect == 301) header("HTTP/1.1 301 Moved Permanently"); elseif ($redirect == 302) header("HTTP/1.1 302 Found"); header("Location: ".get_permalink().$ga_tracking); } } else { global $post; // Link relations $http_shortlink_rel = ($options["http_shortlink_rel"] == "checked") ? true : false; if ($http_shortlink_rel) if (($posts_enabled && is_single()) || ($pages_enabled && is_page()) || ($attachments_enabled && is_attachment())) 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; $count_attachments = wp_count_posts('attachment'); $total_attachments = $count_attachments->inherit; $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, "total_attachments" => $total_attachments, )); } 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($_GET["tfl_hide"])) { $options["tfl_core_notice"] = 0; update_option("twitter_friendly_links", $options); } if (isset($_POST["twitter-friendly-links-submit"])) { $options["style"] = $_POST["style"]; $options["format"] = $_POST["format"]; $options["redirect"] = $_POST["redirect"]; $options["posts_enabled"] = $_POST["posts_enabled"]; $options["pages_enabled"] = $_POST["pages_enabled"]; $options["attachments_enabled"] = $_POST["attachments_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["sociable_fix"] = $_POST["sociable_fix"]; $options["ga_tracking"] = $_POST["ga_tracking"]; $options["html_shortlink_rel"] = $_POST["html_shortlink_rel"]; $options["http_shortlink_rel"] = $_POST["http_shortlink_rel"]; $options["rel_canonical"] = $_POST["rel_canonical"]; update_option("twitter_friendly_links", $options); } $style = $options["style"]; $format = $options["format"]; $redirect = $options["redirect"]; $posts_enabled = $options["posts_enabled"]; $pages_enabled = $options["pages_enabled"]; $attachments_enabled = $options["attachments_enabled"]; $twitter_tools_fix = $options["twitter_tools_fix"]; $askapache_google_404 = $options["askapache_google_404"]; $tweet_this_fix = $options["tweet_this_fix"]; $sociable_fix = $options["sociable_fix"]; $ga_tracking = $options["ga_tracking"]; $html_shortlink_rel = $options["html_shortlink_rel"]; $http_shortlink_rel = $options["http_shortlink_rel"]; $rel_canonical = $options["rel_canonical"]; $selected[$redirect] = " selected=\"selected\""; $selected[$format] = " selected=\"selected\""; if ($format == "generic") $link_preview = "123"; elseif ($format = "base32") $link_preview = "7e1"; ?>

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.

/prefix (blank by default)
Generic by default
id="posts_enabled" name="posts_enabled"/> Posts
id="pages_enabled" name="pages_enabled"/> Pages
id="attachments_enabled" name="attachments_enabled"/> Attachments
302 by default

You can tag your destination links for Google Analytics Tracking. For example: utm_source=twitter&utm_medium=shortlink&utm_campaign=shortlinks. You can generate a tagged link using the Google Analytics URL Builder. Do not include the website address in the input box above. Start from utm_source. This string will be appended to the destination address. Leave blank to disable.

Link Relations

Search engines and URL shorteners. Bunch of thoughts on linking relations, so here are the main options.

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.

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 and SexyBookmarks plugins.
id="askapache_google_404" name="askapache_google_404"/> Fix for the AskApache Google 404 plugin.
id="sociable_fix" name="sociable_fix"/> Fix for the Sociable plugin.

List of short links

publish; $count_pages = wp_count_posts('page'); $total_pages = $count_pages->publish; $count_attachments = wp_count_posts('attachment'); $total_attachments = $count_attachments->inherit; ?>
Posts / '; else echo 'Posts (disabled) / '; if ($pages_enabled == "checked") echo ' / '; else echo 'Pages (disabled) / '; if ($attachments_enabled == "checked") echo ''; else echo 'Attachments (disabled)'; ?>
Displaying 1-15 of
have_posts()) { $query->the_post(); $friendly_link = twitter_link($query->post->ID); $i++; ?>
  Title Friendly Link
  Title Friendly Link
Posts are disabled
.
' . "\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; if ($options["format"] == "base32") $friendly_link = get_option("home") . "/" . $style . tfl_base32($post_id); else $friendly_link = get_option("home") . "/" . $style . $post_id; return $friendly_link; } function permalink_to_twitter_link($permalink) { $post_id = url_to_postid($permalink); return twitter_link($post_id); } function tfl_tweet_this_fix($content) { global $post; $twitter_link = twitter_link(); $content = preg_replace("/href=\\\"http:\/\/twitter.com\/home\/?\?status=([^\\\"]+)\\\"/", "href=\"http://twitter.com/home/?status=" . urlencode($post->post_title . " " . $twitter_link) . "\"", $content); return $content; } function tfl_sociable_fix($content) { global $post; $twitter_link = twitter_link(); $content = preg_replace("/href=\\\"http:\/\/twitter.com\/home\/?\?status=([^\\\"]+)\\\"/", "href=\"http://twitter.com/home/?status=" . urlencode($post->post_title . " " . $twitter_link) . "\"", $content); return $content; } function tfl_admin_notices() { echo "

You have to change your permalink structure for Twitter Friendly Links to work (don't use default).

"; } function tfl_admin_notices_core() { echo "

Hey, this version of Twitter Friendly Links had some core changes. Make sure you double check your settings, which may have not been saved from your previous version, okay? Sorry for any inconvenience caused. Hide this message.

"; } register_activation_hook(__FILE__, 'tfl_activate'); register_deactivation_hook(__FILE__, 'tfl_deactivate'); function tfl_deactivate() { //delete_option("twitter_friendly_links"); // Use this only for debug return true; } function tfl_activate() { $options = get_option("twitter_friendly_links"); // Default plugin options $defaults = array( "style" => "", // default style is example.com/123 "format" => "generic", // default format is generic (numbers only) "redirect" => 302, // temporary redirect by default "posts_enabled" => "checked", // posts enabled by default "pages_enabled" => "", // pages disabled by default "attachments_enabled" => "", // attachments disabled by default "twitter_tools_fix" => "", // disabled by deafult "askapache_google_404" => "", "tweet_this_fix" => "", "sociable_fix" => "", "ga_tracking" => "", "html_shortlink_rel" => "", "http_shortlink_rel" => "", "rel_canonical" => "", "tfl_core_notice" => 0, ); foreach($defaults as $key => $default_value) $options[$key] = (!isset($options[$key])) ? $default_value : $options[$key]; update_option("twitter_friendly_links", $options); return true; } function tfl_base32($str, $reverse = false) { if (!$reverse) { $post_id = intval($str); return base_convert($post_id + 10000, 10, 36); } else { $post_id = base_convert($str, 36, 10) - 10000; return $post_id; } } ?>