WordPress 固定链接标题别名自动转换 MD5 码,亲测有效。

IT资讯
IT资讯
IT资讯
1173
文章
239
评论
2022-11-2512:45:14
评论
427 1823字

WordPress 固定链接有多种方式,其中有不少人喜欢设置格式为文章标题所为固定链接形式。

使用文章标题作为固定链接形式,英文还好,如果是中文,则需要手动修改链接别名,否则会变成长长的中文转码,很不好看。

当然,也可以采用自动翻译标题为别名的插件,但是,由于自动翻译会拖慢整个 WordPress 网站。

如果觉得固定链接为标题形式比较麻烦,比如,本博客的就以文章 ID 为固定链接形式。

固定链接的地址设置为 post_id 也有一个弊端,几乎空间每出一次问题,ID就会换上一次,对Google搜索的收录很不好。

因此,有位达人写了这则代码,采用标题的 MD5 码作为固定链接形式,这样子,只要标题不同撞车的机率非常少。

WordPress 固定链接标题别名自动转换 MD5 码,亲测有效。

WordPress 固定链接标题别名自动转换 MD5 码,亲测有效。
WordPress 固定链接标题别名自动转换 MD5 码,亲测有效。
WordPress 固定链接标题别名自动转换 MD5 码,亲测有效。
WordPress 固定链接标题别名自动转换 MD5 码,亲测有效。
WordPress 固定链接标题别名自动转换 MD5 码,亲测有效。

PS: 切记,用这种形式的链接,就要把固定链接当中的 文章ID 改成 PostName 的形式。新发布的文章会自动生效,旧文章需要“更新”一下才能生效(可以用文章批量编辑功能来处理)。

把下面的代码加入 function.php 文件中:

if (!class_exists('wp_slup_md5code')):
class wp_slup_md5code {

    var $post_title = '';
    var $post_type = '';

    function wp_slup_md5code() {
        global $wp_version;

        add_filter('type_save_pre', array(&$this, 'get_post_type'), 0);
        add_filter('title_save_pre', array(&$this, 'get_post_title'), 0);
        add_filter('name_save_pre', array(&$this, 'set_post_name'), 0);

        if ($wp_version > 2.4 && strpos($_SERVER['REQUEST_URI'], 'admin-ajax.php') > 0
                && $_POST['action'] === 'sample-permalink') {

            add_filter('sanitize_title', array(&$this, 'w25_ajax_slug'), 0);
            register_shutdown_function(array(&$this, 'w25_ajax_remove'));
        }
    }

    function get_post_type($type) {
        $this->post_type = $type;
        return $type;
    }

    function get_post_title($title) {
        $this->post_title = $title;
        return $title;
    }

    function set_post_name($name) {
        if (strcmp('post', $this->post_type) == 0) {
            $name = $this->md5_encoding($this->post_title);
        }
        return $name;
    }

    function w25_ajax_slug($name) {
        remove_filter('sanitize_title', array(&$this, 'w25_ajax_slug'), 0);
        if (strcmp('post', $_POST['post_type']) == 0) {
            $name = $this->md5_encoding($_POST['new_title']);
        }
        add_filter('sanitize_title', array(&$this, 'w25_ajax_slug'), 0);
        return $name;
    }

    function w25_ajax_remove() {
        remove_filter('sanitize_title', array(&$this, 'w25_ajax_slug'), 0);
    }

    private function md5_encoding($str) {
        $str = trim(strip_tags($str));
        if (strlen($str) == 0) {
            return $str;
        }

        $str = mb_convert_encoding($str, 'UTF-8');

        $md5_str = md5($str);
        $md5_str = substr($md5_str, 8, 16);
        return $md5_str;
    }
}

endif;

$wp_slup_md5code = new wp_slup_md5code();

 

 

 

  • Copyright ©  PC在线云端  版权所有.
  • 转载请务必保留本文链接:https://nrcs.xyz/web/wordperss_code/11542.html
Orgorg速率最高可达1000Mbps,流畅观看Youtube 4K、TikTok,支持 Windows、Android、iOS、Mac,支持 微信、支付宝 付款!
全球数据中心,多点BGP保证速度,无视晚高峰,全天4K秒开,IPLC专线无惧封锁
全IEPL /青云跨境,高峰时期稳定8K播放,流媒体影视, ChatGPT 解锁保障,客户端无日志保护您的隐私安全,稳定运行5年+
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: