Wordpress:在网址中包含语言变量

Mic*_*ert 17 wordpress permalinks url-rewriting

我一直在为wordpress开发一个完全有效的语言插件.现在唯一缺少的是网址重写.我一直在关注stackoverflow上的很多网站,资源和其他问题,但我似乎无法让我的永久链接工作.

我已经能够像这样添加一个查询字符串变量:

public function append_query_string($url) 
{
    $args = array('lang' => $this->get_locale());
    return add_query_arg($args, $url);
}
add_filter('page_link', array($this, 'append_query_string'));
add_filter('post_link', array($this, 'append_query_string'));
add_filter('the_permalink', array($this, 'append_query_string'));
Run Code Online (Sandbox Code Playgroud)

这会改变我的链接http://www.mylink.com?lang=en_us,例如.我现在想要的是添加一个permastruct,以便用户可以拥有漂亮的url(例如http://www.mylink.com/en/)

我添加了以下代码:

public function add_query_var($vars)
{
    $vars['lang'] = $this->get_locale();
    return $vars;
}
add_filter('request' , array($this, 'add_query_var'), 10, 2 );

public function custom_permastruct() 
{
    add_permastruct('language', '%lang%', false);
}
add_action('wp_loaded', array($this, 'custom_permastruct'));
Run Code Online (Sandbox Code Playgroud)

我现在唯一需要的是重写规则,我猜想,但我可能完全错了.谁知道添加这个permastruct的最佳解决方案是什么?

编辑 我一直试图让这个工作一个月,我似乎无法掌握永久链接,甚至没有所有以前的答案和我自己的研究.所以这就是为什么我再次以赏金来发布这篇文章的原因.我需要的是:我有一个返回语言代码的函数(get_locale).该语言代码应在我的URL中实现,如下所示:"http://www.mywebsite.com/LANGUAGE_HERE/..."

我知道我需要为此注册我自己的永久链接结构,但这就是一切都出错的地方.我需要什么过滤器以及我应该在过滤器功能中添加什么?非常感谢任何帮助,因为我在这里非常绝望.

编辑2

所以我添加了重写规则,但它们似乎也没有用.我在这里有点绝望.无论如何,这是重写规则的代码:

public function add_rewrite_rules()
{   
    $languages = $this->get_all_languages();
    foreach($languages as $language) {
        add_rewrite_rule('^' . $language->code . '/([^/]*)/?$', 'index.php?lang=$matches[1]', 'top');
    }
}
add_action('init', array($this, 'add_rewrite_rules'));
Run Code Online (Sandbox Code Playgroud)

Sjo*_*jon 6

正确设置Wordpress 2.0+会将所有请求重定向到/index.php,因此它不需要任何htaccess更新,并且您的注册perma-struct看起来很好.我认为,所有剩下的就是配置WordPress的使用你的%lang结构使用自定义的结构,你应该是好去


MMK*_*MMK 1

好吧好吧,这是一个实现您所要求的代码块。

public function init(){

    $permalink_structure = get_option( 'permalink_structure' );

    if( $permalink_structure != '' ){

        global $wp_rewrite;

        $lang = '/' . get_locale();

        if ( ! got_url_rewrite() )
            $prefix = '/index.php';

        if ( is_multisite() && !is_subdomain_install() && is_main_site() )
            $blog_prefix = '/blog';


        if ( ! empty( $permalink_structure ) ) {

            $permalink_structure = preg_replace( 
                '#/+#',
                '/',
                '/' . str_replace( '#', '', $permalink_structure )
            );

            if ( $prefix && $blog_prefix )
                $permalink_structure = $prefix . preg_replace( 
                    '#^/?index\.php#',
                    '',
                    $permalink_structure
                );
            else
                $permalink_structure = $blog_prefix . $permalink_structure;
        }


        if( substr( $permalink_structure, 0, strlen($lang) ) !== $lang ){
            $permalink_structure = $lang . $permalink_structure;
        }

        $wp_rewrite->set_permalink_structure( $permalink_structure );

    }          
}
Run Code Online (Sandbox Code Playgroud)

笔记:

1)确保您init在钩子中使用(您可以为该函数指定任何名称)函数init

2)在wp-admin文件夹中查找options-permalink.php. 从这一行开始,75 您将看到一些有趣的代码,它们构成了这个答案的基础。

3)您可能还想阅读有关法典的这篇文章

上述代码不需要用户手动选择永久链接结构。使用的任何永久链接结构都将以locale.