自定义重写规则不起作用

时间:2016-11-30 作者:Pipo

我正在尝试从此类url获取:

domain.com/page/classes/?language=english&need=pro
对此:

domain.com/class/english-pro
<小时>

MORE INFORMATIONS

这两个变量都添加为query_vars. classes 是当前页,并且page 是父页。

<小时>

WHAT I\'VE TRIED

这是我尝试过的重写规则,但它不起作用。

function custom_rewrite() {
    add_rewrite_rule( \'class/([^/]+)-([^/]+)$\', \'index.php?language=$matches[1]&need=$matches[2]\', \'top\' );
}
add_action( \'init\', \'custom_rewrite\' );
由于某些原因,我无法理解regex表达式。。。谢谢你的帮助!

<小时>

UPDATE 1

经过一番探索,我发现:

function custom_rewrite() {
  add_rewrite_tag( \'%_language%\', \'([a-zA-Z\\d\\-_+]+)\' );
  add_rewrite_tag( \'%_need%\', \'([a-zA-Z\\d\\-_+]+)\' );
  add_rewrite_rule( \'class/([a-zA-Z\\d\\-_+]+)-([a-zA-Z\\d\\-_+]+)?\', \'index.php?_language=$matches[1]&_need=$matches[2]\', \'top\' );
}
add_action( \'init\', \'custom_rewrite\' );
但它仍然不起作用。

<小时>

UPDATE 2

我做了一些更改,以便更容易进行重定向。其工作原理如下:

我有两个选择,人们可以选择language 和aneed. 两者都是声明的查询变量表单验证时加载此url:domain.com/page/classes/?_language=english&_need=pro.language-need. 我通过使用query_posts 建造namequery variables.domain.com/page/classes/?language=english&need=pro) 指向此新url(domain.com/class/english-pro) 同时仍加载相同的信息**。

<小时>

UPDATE 3

我在下面尝试了@jgraup解决方案(没有prefix__pre_post_link 功能,因为我已经简化了我的url,不需要它),但它不起作用(我的自定义帖子类型使用相同的设置

if ( ! class_exists( \'PrefixClassesRewrites\' ) ) {

  class PrefixClassesRewrites {

    const POST_TYPE_SLUG = \'lang\';

    function __invoke() {
      add_action( \'init\', array ( $this, \'prefix__init\' ) );
      add_action( \'pre_get_posts\', array ( $this, \'prefix__pre_get_posts\' ) );
    }

    public function prefix__init() {
      // custom query params that we check for later
      add_rewrite_tag( \'%_language%\', \'([a-zA-Z\\d\\-_+]+)\' );
      add_rewrite_tag( \'%_need%\', \'([a-zA-Z\\d\\-_+]+)\' );
      add_rewrite_tag( \'%_page_class%\', \'_page_class\' );

      // rewrite rule to transform the URL into params
      add_rewrite_rule( \'class/([a-zA-Z\\d\\-_+]+)-([a-zA-Z\\d\\-_+]+)?\', \'index.php?_language=$matches[1]&_need=$matches[2]&_page_class=1\', \'top\' );

      flush_rewrite_rules(); // <-- for testing only | removed once the rewrite rules are in place
    }

    public function prefix__pre_get_posts( $query ) {

      if ( isset( $query->query_vars[ \'_page_class\' ] ) ) {

        // Convert the variables to a custom post type name.
        $name = implode( \'-\', array (
          $query->query_vars[ \'_langue\' ],
          $query->query_vars[ \'_besoin\' ],
        ) );

        // Add the name and post_type to the query.
        $query->query_vars[ \'name\' ]      = $name;
        $query->query_vars[ \'post_type\' ] = array ( static::POST_TYPE_SLUG );

        // Nothing else to do here.
        return;
      }
    }

  }

  $prefixClassesRewrites = new PrefixClassesRewrites();
  $prefixClassesRewrites(); // kick off the process
}
问题是什么都没有改变,我的url没有被重写。顺便问一下,重写tule在工作时应该在htaccess中吗?

3 个回复
最合适的回答,由SO网友:Pipo 整理而成

好吧,我改变了对这个问题的思考方式,因为我真的不知道问题来自哪里,所以我选择了另一种解决方案。

I am using a wp_safe_redirect to the url of the corresponding custom post type.

对于那些对解决方案感兴趣的人,我的表单操作是permalink 一个专门为此创建的页面(我们称之为class)。

在这个类中,我有以下安全重定向:

// getting my variables from the url
$language = get_query_var( \'_language\' );
$need = get_query_var( \'_need\' );

// redirecting to the corresponding custom post type url
if( isset( $language ) && isset( $need ) ) {
  wp_safe_redirect( esc_url( home_url(\'/\') ) .\'class/\'. $language .\'-\'. $need .\'/\', 301 );
  exit;
} else {
  // if variables are not defined 
  // redirect to home (or whatever page you want)
  wp_safe_redirect( esc_url( home_url(\'/\') ) );
  exit;
}
现在一切都很好!

SO网友:Varun Kumar

希望这会像对我一样对我有用。

add_filter(\'rewrite_rules_array\', \'insert_custom_rules\');

add_filter(\'query_vars\', \'insert_custom_vars\');


function insert_custom_vars($vars){        

    $vars[] = \'main_page\';       

    return $vars;

}

function insert_custom_rules($rules) {

    $newrules = array();

    $newrules=array(
        \'page-name/(.+)/?\' => \'index.php?pagename=page-name&main_page=$matches[1]\'
    );

    return $newrules + $rules;
}
因此,在get\\u query\\u var()函数的帮助下,我们可以从url中获取值并相应地使用。通过这种方式,我们可以获得漂亮的URL。

注意:如果您有多个规则,请先编写变量多的规则,然后编写变量少的规则。如果有多个变量,请指定变量之间的静态内容。

SO网友:jgraup

这里有很多假设,但也许你可以澄清一下。这假设您制作的CPT可能如下所示:

domain.com/page/class/english-pro
并将其转换为

domain.com/page/class-english-pro
不管怎样。URL正在获取参数并修改查询以显示页面。这里添加了CPT,所以这个示例可以适用于任何开箱即用的人。添加permalink重写是为了调整permalink。

我不会说这就是答案,但这里有些东西可能会对你有所帮助。

<?php

if ( ! class_exists( \'PrefixClassesRewrites\' ) ) {

    class PrefixClassesRewrites {

        const POST_TYPE_SLUG = \'classes\';

        function __invoke() {
            add_action( \'init\', array ( $this, \'prefix__init\' ) );
            add_action( \'init\', array ( $this, \'prefix__init_cpt\' ), 0 );
            add_action( \'pre_get_posts\', array ( $this, \'prefix__pre_get_posts\' ) );
            add_filter( \'post_type_link\', array ( $this, \'prefix__pre_post_link\' ), 3 );
            add_filter( \'post_link\', array ( $this, \'prefix__pre_post_link\' ), 3 );
        }

        /**
         * Register the custom post type.
         */
        public function prefix__init_cpt() {
            $rewrite = array (
                \'slug\'       => \'page/class\', // get\'s closer but will end with / not -
                \'with_front\' => false,
                \'pages\'      => true,
                \'feeds\'      => true,
            );

            register_post_type( static::POST_TYPE_SLUG, array (
                \'label\'               => __( \'Classes Post Type\', \'text_domain\' ),
                \'description\'         => __( \'Classes Post Type Description\', \'text_domain\' ),
                \'rewrite\'             => $rewrite,
                \'hierarchical\'        => false,
                \'public\'              => true,
                \'show_ui\'             => true,
                \'show_in_menu\'        => true,
                \'menu_position\'       => 5,
                \'show_in_admin_bar\'   => true,
                \'show_in_nav_menus\'   => true,
                \'can_export\'          => true,
                \'has_archive\'         => true,
                \'exclude_from_search\' => false,
                \'publicly_queryable\'  => true,
                \'capability_type\'     => \'page\',
            ) );
        }

        /**
         * Add the rewrite rule and allow custom query params
         */
        public function prefix__init() {
            // custom query params that we check for later
            add_rewrite_tag( \'%_page_class%\', \'_page_class\' );
            add_rewrite_tag( \'%_language%\', \'([a-zA-Z\\d\\-_+]+)\' );
            add_rewrite_tag( \'%_need%\', \'([a-zA-Z\\d\\-_+]+)\' );

            // rewrite rule to transform the URL into params
            add_rewrite_rule( \'page/class-([a-zA-Z\\d\\-_+]+)-([a-zA-Z\\d\\-_+]+)?\', \'index.php?_page_class=1&_language=$matches[1]&_need=$matches[2]\', \'top\' );

            flush_rewrite_rules(); // <-- for testing only | removed once the rewrite rules are in place
        }

        /**
         *  Modify the query to convert our params to a custom post type pagename
         *
         * @param $query
         */
        public function prefix__pre_get_posts( $query ) {

            // Check for our custom query param. Really just a flag to do this
            if ( isset( $query->query_vars[ \'_page_class\' ] ) ) {

                // Convert the variables to a custom post type name.
                $name = implode( \'-\', array (
                    $query->query_vars[ \'_language\' ],
                    $query->query_vars[ \'_need\' ],
                ) );

                // Add the name and post_type to the query.
                $query->query_vars[ \'name\' ]      = $name;
                $query->query_vars[ \'post_type\' ] = array ( static::POST_TYPE_SLUG );

                // Nothing else to do here.
                return;
            }
        }

        /**
         * Rewrite the permalink to match our setup.
         *
         * @param string       $permalink
         * @param null|WP_Post $post
         * @param bool         $leavename
         *
         * @return string
         */
        public function prefix__pre_post_link( $permalink = \'\', $post = null, $leavename = false ) {
            return str_replace( \'/page/class/\', \'/page/class-\', $permalink );
        }
    }

    $prefixClassesRewrites = new PrefixClassesRewrites();
    $prefixClassesRewrites(); // kick off the process
}

相关推荐