Implementing a URL Shortener

时间:2019-08-29 作者:Condor

我想创建一个URL缩短器,其中我使用REST API端点请求来获取缩短的URL,例如:

mywebsite.com/wp-json/wl/v1/shorten?url=url-to-shorten.com

例如,我收到一个带有我发送到上面URL的URL的缩写版本的响应mywebsite.com/akjv3, 重定向到url-to-shorten.com.

我的问题是,使用标准的WordPress方法是否可以做到这一点?我查看了一些文档,但只能找到以下内容:

Short URL

Short Link

据我所知,这不是我想要的功能。

我正在努力实现的示例:

function wl_shortcode() {
    if ( isset( $_GET[\'url\']) ) {
        $shortcode = isset( $_GET[\'url\'] )  ? esc_attr( $_GET[\'url\'] )   : \'\';

        // CODE HERE TO SHORTEN URL


        return $shorturl;
    } else {
         return false;
    }
}

add_action(\'rest_api_init\', function() {
    register_rest_route(\'wl/v1\', \'shorturl\', [
        \'methods\' => \'GET\',
        \'callback\' => \'wl_shortcode\',
    ]);
});
我正在考虑如上所述创建端点,其中URL与唯一键一起插入到DB表中。访问时mywebsite.com/avuy3, 例如,我应该重定向到mywebsite.com/some/path/to/a/page. 有没有人能在这方面指导我?

1 个回复
SO网友:orr burgel

Step 1: Create a new template file in your themes folder named: shorturl.php

<?php
/*
Template Name: Short URLS
Template Post Type: post, page
*/
while ( have_posts() ) :
    the_post();
    $content=get_the_content();
    header(\'Location: \'.str_replace("&amp;","&",$content));
    exit();
endwhile;

Step 2: Create a new php file in your wp-content/plugins/ folder and name it: public-url-shortener.php

<?php
/*
Plugin Name: Public URL Shortener
Plugin URI:
Description: Use the shortcode [urlshortener] in a page to use this plugin.
Version: 1
Author: Burgil
Author URI: https://wordpress.org/plugins/public-url-shortener
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
/**
 * Front end registration
 */
function urlshortener_func( $atts ){
    if(isset($_POST[\'url\'])&&isset($_POST[\'custom\'])){
        $payload=\'
        <form method="post">
        <input class="input" type="text" value="\'.$_POST[\'url\'].\'" name="url" placeholder="Paste long url and shorten it">
        <div class="customurl"><label>\'.get_home_url()."/".\'</label><input id="customkey" class="input" type="text" value="\'.$_POST[\'custom\'].\'" name="custom" placeholder="Enter a custom link"></div>
        <input class="button button-primary" id="shortbtn" style="display: inline;" type="submit" value="Shorten">
        </form>
        \';
        $isEmpty=false;
        if(empty($_POST[\'custom\'])){$payload=$payload."<h3 style=\'color:red;border:3px double;width:auto;background: #0000007a;border-radius: 10px;\'>ERROR: You entered an empty Custom Link for your shorten URL!</h3>";$isEmpty=true;}
        if(empty($_POST[\'url\'])){$payload=$payload."<h3 style=\'color:red;border:3px double;width:auto;background: #0000007a;border-radius: 10px;\'>ERROR: You entered an empty URL!</h3>";$isEmpty=true;}
        if($isEmpty==false){
            $urltoshort=$_POST[\'url\'];
            $custom_link = sanitize_title_with_dashes( remove_accents( $_POST[\'custom\'] ) );
            $new_post = array(
                \'post_title\' => $custom_link,
                \'post_content\' => $urltoshort,
                \'post_status\' => \'publish\',
                \'post_date\' => date(\'Y-m-d H:i:s\'),
                \'post_author\' => \'\',
                \'post_type\' => \'post\',
                \'post_category\' => array(0)
            );
            if(post_exists( $custom_link )==0){
                $post_id = wp_insert_post( $new_post );
                update_post_meta( $post_id, \'_wp_page_template\', \'shorturl.php\' );
                $payload=$payload."<h3 style=\'color:green;border:3px double;width:auto;background: #0000007a;border-radius: 10px;\'>SUCCESS: Custom Link Created: <a href=\'".get_home_url()."/".$custom_link."\'>".get_home_url()."/".$custom_link."</a></h3>";
            }else{
                $payload=$payload."<h3 style=\'color:red;border:3px double;width:auto;background: #0000007a;border-radius: 10px;\'>ERROR: Custom Link \'".get_home_url()."/".$custom_link."\' Already Exists!</h3>";
            }
        }
    }else{
        $payload=\'
        <form method="post">
        <input class="input" type="text" value="" name="url" placeholder="Paste long url and shorten it">
        <div class="customurl"><label>https://www.gtamacro.ga/</label><input id="customkey" class="input" type="text" value="" name="custom" placeholder="Enter a custom link"></div>
        <input class="button button-primary" id="shortbtn" style="display: inline;" type="submit" value="Shorten">
        </form>
        \';
    }
    return $payload;
}
add_shortcode( \'urlshortener\', \'urlshortener_func\' );

Step 3: Activate The Plugin Public URL Shortener

Step 4: Create a new page in your WordPress and name it URL Shortener

Step 5: Add the following shortcode in the content: [urlshortener]

Now open the page you created, make sure to choose a template that has the get_content() function in it and enjoy.

enter image description here