WP rewrite_rules_array挂钩中的错误模式

时间:2012-06-07 18:53:37

标签: php regex url-rewriting wordpress-plugin

使用永久链接的WP编码人员将熟悉以下代码块。

add_filter('rewrite_rules_array', array($this, 'insert_rewrite_rules'));
function insert_rewrite_rules($rules)
{
    // $link_cloaking_text is properly set

    $newrules = array();
    $newrules['([^\/]*)/\b'.$link_cloaking_text.'\b/(\d*)$'] =    
    'index.php?pagename=$matches[1]&link_cloaking_id=$matches[2]';

    return $newrules + $rules;
}

问题:我无法获得pagenamelink_cloaking_id个值(query_vars挂钩和$wp_rewrite->flush_rules()已正确使用)

$newrules['([^\/]*)/\b'.$link_cloaking_text.'\b/(\d*)$'] =     
'index.php?pagename=$matches[1]&link_cloaking_id=$matches[2]';

debug:
    WP_Query::__set_state(array(
       'query_vars' => 
      array (
        'error' => '404',
        'm' => 0,
        'p' => 0,
        'post_parent' => '',
        'subpost' => '',
        'subpost_id' => '',
        'attachment' => '',
        'attachment_id' => 0,
        'name' => '',
        'static' => '',
        'pagename' => '', // empty
        'page_id' => 0,

但如果我将$matches[1]替换为常量my_pagename,则可以使用:

$newrules['([^\/]*)/\b'.$link_cloaking_text.'\b/(\d*)$'] =      
'index.php?pagename=my_pagename&link_cloaking_id=$matches[2]';

debug:
    WP_Query::__set_state(array(
       'query_vars' => 
      array (
        'pagename' => 'my_pagename', // correct
        'link_cloaking_id' => '8', // correct
        'error' => '',
        'm' => 0,
        'p' => 0,
        'post_parent' => '',

我做错了什么?

0 个答案:

没有答案