URL shortening is a technique in which a URL is made substantially shorter in length and still links to the required page. This is achieved by using a redirect on a domain name that is short, which links to the web page that has the longer URL.
URL缩短是一种使URL的长度大大缩短并且仍然链接到所需页面的技术。 这是通过在短域名上使用重定向来实现的,该域名链接到具有较长URL的网页。
In this tutorial, I’ll show you how to create a URL shortening plugin for WordPress using Google’s URL Shortener API.
在本教程中,我将向您展示如何使用Google的URL Shortener API为WordPress创建URL缩短插件。
Before we start building a URL shortening plugin let’s see a few examples as to why we may need this plugin:
在开始构建URL缩短插件之前,让我们看一些为什么我们可能需要此插件的示例:
QR codes have a character limit. Very long URLs don’t fit, therefore shortening of the URL is required.
QR码有字符限制。 非常长的URL不适合,因此需要缩短URL。
To use the Google URL Shortener API you’ll need to acquire an API Key. This API key is used by Google to keep track of your application.
要使用Google URL Shortener API,您需要获取一个API密钥。 Google使用此API密钥来跟踪您的应用程序。
Here are the steps to acquire your API key:
以下是获取API密钥的步骤:
Visit Google Developers Console.
访问Google Developers Console 。
Our plugin will contain one directory and one file. Here is the structure:
我们的插件将包含一个目录和一个文件。 结构如下:
--url-shortener
-url-shortener.php
To make the plugin installable, we put this code in the url-shortener.php file:
为了使该插件可安装,我们将以下代码放在url-shortener.php文件中:
/*
Plugin Name: URL Shortener
Plugin URI: http://www.sitepoint.com
Description: Create's a Shortened URL of every post.
Version: 1.0
Author: Narayan Prusty
*/
We need to create a settings page for our plugin where administrator’s can enter the URL Shortener API key. Here is the code to create a settings page using the WordPress Settings API:
我们需要为插件创建一个设置页面,管理员可以在其中输入URL Shortener API密钥。 这是使用WordPress设置API创建设置页面的代码:
function url_shortener_settings_page()
{
add_settings_section("section", "Enter Key Details", null, "url-shortener");
add_settings_field("url-shortener-input-field", "API Key", "url_shortener_input_field_display", "url-shortener", "section");
register_setting("section", "url-shortener-input-field");
}
function url_shortener_input_field_display()
{
?>
" />
}
add_action("admin_init", "url_shortener_settings_page");
function url_shortener_page()
{
?>URL Shortener Setting
settings_fields("section");
do_settings_sections("url-shortener");
submit_button();
?>
}
function menu_item()
{
add_submenu_page("options-general.php", "URL Shortener", "URL Shortener", "manage_options", "url-shortener", "url_shortener_page");
}
add_action("admin_menu", "menu_item");
We are storing the API key as a WordPress option with the name url-shortener-input-field
.
我们将API密钥作为WordPress选项存储为名称url-shortener-input-field
。
This is what the settings page should look like:
设置页面应如下所示:
To shorten a long URL you need to send a POST request to the https://www.googleapis.com/urlshortener/v1/url
URL with your API key and long URL.
要缩短长网址,您需要使用API密钥和长网址将POST请求发送到https://www.googleapis.com/urlshortener/v1/url
网址。
Here’s what a sample request looks like:
这是一个示例请求的样子:
POST https://www.googleapis.com/urlshortener/v1/url
Content-Type: application/json
{"longUrl": "https://www.sitepoint.com/"}
Here is the sample response looks like:
这是示例响应,如下所示:
{
"kind": "urlshortener#url",
"id": "http://goo.gl/fqsT",
"longUrl": "https://www.sitepoint.com/"
}
Note: You cannot send more than 1 million requests using the same API key in a day.
注意:每天使用同一API密钥发送的请求不能超过一百万。
We want to display the shortened URL in a meta box in the post edit screen. For creating a meta box, we’ll use the Meta Box API and for the URL shortening we’ll use the WordPress HTTP API.
我们想在帖子编辑屏幕的一个元框中显示缩短的URL。 为了创建一个meta box,我们将使用Meta Box API ,对于URL缩短,我们将使用WordPress HTTP API 。
Here’s the code to display our shortened URL in a meta box:
这是在元框中显示缩短的URL的代码:
function url_shortener_meta_box_markup($object)
{
$key = get_permalink($object->ID);
if(get_option('url-shortener-input-field', '') != "")
{
if(get_option($key, "") != "")
{
echo get_option($key, "");
return;
}
$url = 'https://www.googleapis.com/urlshortener/v1/url';
$result = wp_remote_post(
add_query_arg(
'key',
get_option('url-shortener-input-field'),
'https://www.googleapis.com/urlshortener/v1/url'
),
array(
'body' => json_encode(array('longUrl' => esc_url_raw($key))),
'headers' => array( 'Content-Type' => 'application/json')
)
);
if(is_wp_error($result)){echo "Error"; return;}
$result = json_decode($result['body']);
$shortlink = $result->id;
update_option($key, $shortlink);
echo $shortlink;
}
}
function url_shortener_meta_box()
{
add_meta_box("url-shortener-meta-box", "Shorten URL", "url_shortener_meta_box_markup", "post", "side", "default", null);
}
add_action("add_meta_boxes", "url_shortener_meta_box");
Here’s how this code works:
此代码的工作方式如下:
We created a meta box using the add_meta_box
function.
我们使用add_meta_box
函数创建了一个meta框。
We’re retrieving the long URL of the post using the get_permalink()
function.
我们使用get_permalink()
函数检索帖子的长URL。
This is what the meta box looks like in post edit screen:
这是元框在帖子编辑屏幕中的样子:
We also want to display the shortened URL below every post. Here’s the code to do just that:
我们还希望在每个帖子下方显示缩短的URL。 这是执行此操作的代码:
Shortern URL: " . get_option($key, "") . " Shortern URL: " . $shortlink . "function url_shortener_content_filter($content)
{
if($GLOBALS['post']->post_type != "post")
return;
$key = get_permalink($GLOBALS['post']->ID);
if(get_option('url-shortener-input-field', '') != "")
{
if(get_option($key, "") != "")
{
$content = $content . "
return $content;
}
$url = 'https://www.googleapis.com/urlshortener/v1/url';
$result = wp_remote_post(
add_query_arg(
'key',
get_option('url-shortener-input-field'),
'https://www.googleapis.com/urlshortener/v1/url'
),
array(
'body' => json_encode(array('longUrl' => esc_url_raw($key))),
'headers' => array( 'Content-Type' => 'application/json')
)
);
if(is_wp_error($result)){echo "Error"; return;}
$result = json_decode($result['body']);
$shortlink = $result->id;
update_option($key, $shortlink);
$content = $content . "
}
return $content;
}
add_filter("the_content", "url_shortener_content_filter");
This is how this code works:
这段代码是这样工作的:
Here is how it looks on front end:
这是前端的外观:
Now we’re done with building a awesome URL shortener plugin for WordPress!
现在,我们已经为WordPress构建了一个很棒的URL缩短器插件!
If you’d like to check out existing plugins, two of the most popular URL Shortener plugins in the WordPress.org Plugin Directory are URL Shortener and WP URL Shorten.
如果您想查看现有的插件,WordPress.org插件目录中最流行的两个URL Shortener插件是URL Shortener和WP URL Shorten 。
WP URL Shorten uses ref.li to shorten URLs. Ref.li provides real time stats and other traffic information of people visiting your site via their shortened URL.
WP URL Shorten使用ref.li来缩短URL。 Ref.li通过缩短的URL提供访问您网站的人员的实时统计信息和其他流量信息。
URL Shortener plugin lets you choose between Bit.ly, Su.pr, YOURLS, Goo.gl and many other services. This plugin can also generate QR Codes.
短网址插件可以让你Bit.ly,Su.pr,YOURLS,Goo.gl和许多其他服务之间进行选择。 该插件还可以生成QR码。
In this tutorial, I’ve shown you how to easily build your own URL shortening plugin. You can now go ahead and expand on this to add more features such as QR code support and use other URL shortening services. Please share your experience with your own plugins below.
在本教程中,我向您展示了如何轻松构建自己的URL缩短插件。 现在,您可以继续进行扩展,以添加更多功能,例如QR码支持和使用其他URL缩短服务。 请在下面与您自己的插件分享您的经验。
翻译自: https://www.sitepoint.com/create-a-url-shortener-plugin-for-wordpress/