"", // default style is example.com/123 "redirect" => 302, // temporary redirect by default "pages_enabled" => "", // pages disabled by default ); 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() { $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)) { $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()); } } } } function twitter_friendly_links_admin_scripts() { $query = new WP_Query("posts_per_page=-1"); $total_posts = $query->post_count; $query = new WP_Query("posts_per_page=-1&post_type=page"); $total_pages = $query->post_count; $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"]; update_option("twitter_friendly_links", $options); } $style = $options["style"]; $redirect = $options["redirect"]; $pages_enabled = $options["pages_enabled"]; $selected[$redirect] = " selected=\"selected\""; ?>

Twitter Friendly Links

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

List of short links

post_count; $query = new WP_Query("posts_per_page=-1&post_type=page"); $total_pages = $query->post_count; ?>
/ 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; }