您的位置: 首页 / PHP / bbPress 中文 Tag 问题的解决

bbPress 中文 Tag 问题的解决

Published at Feb 3, 10pm / Keywords:

BBPress

bbPress 是 WordPress 创始人 Matt 设计的轻量级论坛程序。这个论坛以其轻巧,高效以及非常容易可以和 Wordpress 集成,自从推出便深受喜爱,越来越多的被使用。但对于中文用户它一直有一个恼人的“顽疾”,就是无法良好的支持多字节 Tag。最近我在开发一个应用时需要用到 bbPress,今天花了一些时间解决了这个问题。

直接说解决方案,前两个修改之处来自 Blogbeta 的热心网友以及 bbPress 中文论坛

第一处:bb-includes/formatting-functions.php 中函数 sanitize_with_dashes 修改为:

  1. <?php
  2. function sanitize_with_dashes( $text ) {
  3.   $text = strip_tags($text);
  4.   $text = remove_accents($text);
  5.   $text = strtolower($text);
  6.   $text = preg_replace('/&(^x80-xff)+?;/', '', $text); // kill entities
  7.   $text = preg_replace('/[^a-z0-9\x80-\xff _-]/', '', $text);
  8.   $text = preg_replace('/s+/', '-', $text);
  9.   $text = preg_replace(array('|-+|', '|_+|'), array('-', '_'), $text); // Kill the repeats
  10.   return $text;
  11. }
  12. ?>

第二处:bb-includes/gettext.php 112 行改为:

  1. <?php
  2.     if ($magic == ($MAGIC1 & 0xFFFFFFFF) ||$magic==$MAGIC1) { // to make sure it works for 64-bit platforms
  3. ?>

后面这两处是我自己修改的,现在在 bbs.blogbeta 上已经调试通过,但没有经过大量的测试:

第三处:bb-includes/formatting-functions.php 中函数 tag_sanitize 修改为:

  1. <?php
  2. function tag_sanitize( $tag ) {
  3.     $tag = urldecode($tag);
  4.     return sanitize_with_dashes( $tag );
  5. }
  6. ?>

第四处:bb-includes/functions.php 中找到

  1. <?php
  2. if ( 1 === bb_get_option( 'debug' ) ) :
  3.     echo "<table>\n<tr><td>". __('REQUEST_URI') .":</td><td>";
  4.     var_dump($uri);
  5. ?>

在前面添加这样一行:

  1. <?php
  2. if (is_tag()) {
  3.     $check = str_replace($tag->tag,urlencode($tag->tag),$check);
  4. }
  5. ?>

修改完成后测试,已经可以正常添加和删除中文 Tag,并可以根据中文 Tag 查询。

收藏和分享本文 17fav 收藏本文

发表您的观点或推荐本文 Loading...

6 Responses

  1. Feb 4, 11am / LINK / REPLY
    Gravatar

    赞一下,bbPress不支持中文tag的确阻止了许多使用者。
    是否考虑提供一个diff文件或者给bbPress官方申请打一个patch呢?
    :smile:

  2. wenkong
    Mar 6, 8am / LINK / REPLY
    Gravatar

    我用的是 0.8的版本,它不支持中文tag,点选中文字的tag会一直loading页面,
    所以我按照上面的方法修改了文件,可是所有中文的tag链接都变成这样的链接url了,点选之后全没链接上相关的文章啊。
    myblogexample.com/bbpress/tags.php?tag=

    tags.php?tag=(之后没有任何代号了)

  3. Mar 6, 8am / LINK / REPLY
    Gravatar

    @wenkong: 这种方法只适合 0.8 以下的版本,0.8 及其以上的版本已经支持中文 tag 不需要修改了

  4. wenkong
    Mar 6, 4pm / LINK / REPLY
    Gravatar

    iStef, 我安装最新的0.8.1版本,可是点选了中文tag链接不上,是有可能什么原因造成呢?

  5. Mar 6, 4pm / LINK / REPLY
    Gravatar

    @wenkong: 你在 config.php 中添加 $bb->debug = true; 看一下输出信息,固定链接应该是一致的才对。

Now, It's your Turn!

BACK TO Article / Comments