"", // 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\""; ?>
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.
| Title | Friendly Link | |
|---|---|---|
| Title | Friendly Link | |
| =$i;?>. | the_title();?> |
= $friendly_link; ?> |
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 ""; } 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; }