在此方案中使用WP重写重写自定义URL

时间:2012-07-27 作者:Ken

我已经构建了一个插件,可以使用短代码在页面上输出特殊的php页面。

插件目录由以下php页面组成。

product_info.php
index.php
account.php
login.php
checkout.php
core.php
启用的shortcode函数core.php 具体如下:,

function output_store(){
if(!isset($_REQUEST[\'slug\']))
    include(WP_PLUGIN_DIR.\'/\'.basename(dirname(__FILE__)).\'/index.php\');       
if($_REQUEST[\'slug\'])
    include(WP_PLUGIN_DIR.\'/\'.basename(dirname(__FILE__)).\'/\'.$_REQUEST[\'slug\']);   
}
function filter($content = \'\') {
    if ( \'\' == $content || !strstr($content, \'[WP_online_store]\') ) { return $content; }
    return preg_replace(\'|(<p>)?(\\n)*[(\\[)WP_online_store(\\])](\\n)*(</p>)?|\', do_shortcode( \'[WP_online_store]\' ), $content);
}
add_shortcode(\'output_store\', \'output_store\');
add_filter(\'the_content\', \'filter\');
[output_store] 将放置在任何页面上,例如store 当我访问store 页面该页面的默认内容将显示index.php 插件目录中的文件。

index.php 文件我有用于访问的链接login.php, product_info.php, account.php and checkout.php 每个都有以下url:

login.php => http://website.com/store/?slug=login.php
product_info.php => http://website.com/store/?slug=product.info.php
account.php => http://website.com/store/?slug=account.php
checkout.php => http://website.com/store/?slug=checkout.php
同时打开index.php 我有从mysql数据库中提取的产品,可以通过以下链接访问http://website.com/store/?slug=product_info.php&products_id=38 等等

现在我想重写这些URL,使其对搜索引擎友好。我想要:

http://website.com/store/?slug=login.php to become http://website.com/store/login
http://website.com/store/?slug=account.php to become http://website.com/store/account
http://website.com/store/?slug=checkout.php to become http://website.com/store/checkout
和所有产品URL:

    like http://website.com/store/?slug=product_info.php&products_id=38 
    to become http://website.com/store/product-info/38
我如何使用wordpress的WP重写来实现这一点?

我在产品信息页面上尝试了以下操作:

// Define permalinks
function add_query_vars($aVars) {
$aVars[] = "products_id";
return $aVars;
}


function add_rewrite_rules($aRules) {
$aNewRules = array(\'product-info/([^/]+)/?$\' => \'index.php?slug=product_info.php&products_id=$matches[1]\');
$aRules = $aNewRules + $aRules;
return $aRules;
}

add_filter(\'query_vars\', \'add_query_vars\');
add_filter(\'rewrite_rules_array\', \'add_rewrite_rules\');

function isset_productinfo() {
  if(isset($wp_query->query_vars[\'products_id\'])) {
   $sMsdsCat = urldecode($wp_query->query_vars[\'products_id\']);
  }
}
add_action(\'init\',\'isset_productinfo\');

// End Special Permalinks
冲洗了我的permalinks,但它不起作用。。当我进入http://website.com/store/product-info/38 然后继续http://website.com/store/ 而不是显示产品页面。我怎样才能解决这个问题?或者你有其他的解决方案吗?

请帮助。。

2 个回复
SO网友:vmassuchetto

在您的情况下,可能是这样的:

add_action(\'rewrite_rules_array\', \'rewrite_rules\');
function rewrite_rules($rules) {
    $new_rules = array(
        \'product/([0-9]+)/?$\' => \'index.php?product=$matches[1]\'
    );
    return $rules + $new_rules;
}

add_action(\'query_vars\', \'add_query_vars\');
function add_query_vars($vars) {
    array_push($vars, \'product\');
    return $vars;
}

add_action(\'template_redirect\', \'custom_template_redirect\');
function custom_template_redirect() {
    global $wp_query, $post;
    if (get_query_var(\'product\')) {
        include(YOURPLUGINDIR . \'templates/product_template.php\');
        exit();
    }
}
在中product_template.php, 您可以访问查询的产品get_query_var(\'product\').

修改规则数组后,需要刷新规则。只需访问http://yoursite/wp-admin/options-permalink.php.

此外,您还可以查看规则测试顺序以及与之匹配的规则:

add_action(\'wp\', \'debug_rules\');
function debug_rules() {
    global $wp, $wp_query, $wp_rewrite;
    echo $wp->matched_rule . \' | \' . $wp_rewrite->rules[$wp->matched_rule];
    print_r($wp_rewrite->rules);
    exit();
}
请尝试让您的插件同时适用于这两种情况(获取变量和自定义重写规则),因为您永远不知道您的用户是否能够运行永久链接。

因此docs 告诉我们:

if ( get_option(\'permalink_structure\') ) {
    /* do everything as above */
} else {
    /* so stuff only with GET variables
}

SO网友:KalenGi

我认为这种情况下的问题是查询参数名称不匹配:

在您的要求中,您有:

http://website.com/store/?slug=product_info.php&product_id=38
在您的实施中,您有:

$aNewRules = array(\'product-info/([^/]+)/?$\' => \'index.php?slug=product_info.php&products_id=$matches[1]\');
现在如果代码输入product_info.php 期望product_id 然后products_id 行不通。

结束

相关推荐