. */ 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"; ?>
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 | |
| Posts are disabled | ||
| . | the_title();?> |
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; 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).
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.