可以使用自定义端点。只需确保在添加/更改重写规则后刷新它,而不是每次加载页面!
这将改变http://yoursite.com/dev/abc/asdfasdfsdf
进入http://yoursite.com/dev/abc/index.php?whatever_you_want_your_index_to_see=asdfasdfsdf
有关重写规则的详细信息:http://code.tutsplus.com/articles/the-rewrite-api-the-basics--wp-25474.
添加重写规则后,如add_rewrite_rule(\'^dev/abc/([^/]+)/?$\', \'index.php?\'.self::ENDPOINT_SYMBOL.\'=1&\' . self::ENDPOINT_PARAM . \'=$matches[1]\', \'top\');
您可以使用这个很棒的插件来检查regex的工作情况:https://wordpress.org/plugins/monkeyman-rewrite-analyzer/
<?php
////////////////////////////////////////////////////////////////////////////////////////
/**
* ONLY DO THIS ONCE!!!!! LIKE AFTER A THEME SWITCH
* FOR THIS DEMO --- MAKE SURE IT\'S WORKING FIRST -- THEN COMMENT THIS OUT!!!
*/
function for_demo_only_after_endpoint_added()
{
flush_rewrite_rules(false);
}
add_action(\'init\', \'for_demo_only_after_endpoint_added\', 99 );
////////////////////////////////////////////////////////////////////////////////////////
if (!class_exists(\'ExampleEndpoint\')) :
class ExampleEndpoint
{
const ENDPOINT_SYMBOL = \'__dev/abc__\';
const ENDPOINT_PARAM = \'special_var\';
public function __construct()
{
add_filter(\'query_vars\', array($this, \'add_query_vars\'), 0);
add_action(\'parse_request\', array($this, \'sniff_requests\'), 0);
add_action(\'init\', array($this, \'add_endpoint\'), 0);
}
public function add_query_vars($vars)
{
$vars[] = self::ENDPOINT_SYMBOL; // use to snif the request
$vars[] = self::ENDPOINT_PARAM;
return $vars;
}
public function add_endpoint()
{
// if you change this, you\'ll need to flush the rewrite rules!
add_rewrite_rule(\'^dev/abc/([^/]+)/?$\', \'index.php?\' . self::ENDPOINT_SYMBOL . \'=1&\' . self::ENDPOINT_PARAM . \'=$matches[1]\', \'top\');
}
public function sniff_requests()
{
global $wp;
if (isset($wp->query_vars[self::ENDPOINT_SYMBOL])) {
$this->handle_request();
exit;
}
}
protected function handle_request()
{
global $wp;
$request_args = array();
// gather your variables
if (isset($wp->query_vars[self::ENDPOINT_PARAM])) {
$request_args[self::ENDPOINT_PARAM] = $wp->query_vars[self::ENDPOINT_PARAM];
}
// redirect to your index.php page - add the extra info as query args
$location = home_url($path = \'/dev/abc/index.php?\' .
\'whatever_you_want_your_index_to_see=\' . $request_args[self::ENDPOINT_PARAM], $scheme = \'http\');
// go now!
$status = 301;
wp_redirect($location, $status);
exit; // fin
}
}
$ep = new ExampleEndpoint();
endif;
在索引中。php您可以检查查询变量是否通过;
<?php
echo "You made it!<pre>";
var_dump($_REQUEST);
echo "<pre>";