admin管理员组文章数量:1327684
I have a unique project that I am trying to prototype where some of a page's assets comes from wordpress, whereas others come in from a request to a remote API.
Currently I use a page called "series" that uses this structure to make a wp_get_remote call to pass data into the page:
site_url/series/?pname=xyz
And would like to change it to be pretty: site_url/series/xyz
So the effect would be the series page is called, and then my request runs to call in the data I need from the endpoint. I need to run this as soon as possible, because the endpoint has data that I would like to set to the meta description, plus the majority of the page content relies on the data I retrieve.
I have been trying to use the hook 'parse_request', but I get a Warning: Cannot modify header information headers already sent by .. pluggable.php
add_action( 'parse_request', 'test_changes' );
function test_changes( $query ) {
$request_uri_string = $_SERVER['REQUEST_URI'];
echo $request_uri_string;
echo "<br>";
if( strpos($request_uri_string, 'series') !== false ){
echo "theres a series here";
unset($query->query_vars);
$query->query_vars[ 'pagename' ] = "series";
$query->query_vars[ 'page' ] = "";
$query->request = "series";
$query->matched_query = "pagename=series&page=";
}
//See what is returned.
echo "<br><br><p>Parse Request</p>";
var_dump($query);
return $query;
}
Trying to get some insight into why this doesn't work, solutions to create the result I want, and perhaps some other ways you can use 'parse_request'.
I have a unique project that I am trying to prototype where some of a page's assets comes from wordpress, whereas others come in from a request to a remote API.
Currently I use a page called "series" that uses this structure to make a wp_get_remote call to pass data into the page:
site_url/series/?pname=xyz
And would like to change it to be pretty: site_url/series/xyz
So the effect would be the series page is called, and then my request runs to call in the data I need from the endpoint. I need to run this as soon as possible, because the endpoint has data that I would like to set to the meta description, plus the majority of the page content relies on the data I retrieve.
I have been trying to use the hook 'parse_request', but I get a Warning: Cannot modify header information headers already sent by .. pluggable.php
add_action( 'parse_request', 'test_changes' );
function test_changes( $query ) {
$request_uri_string = $_SERVER['REQUEST_URI'];
echo $request_uri_string;
echo "<br>";
if( strpos($request_uri_string, 'series') !== false ){
echo "theres a series here";
unset($query->query_vars);
$query->query_vars[ 'pagename' ] = "series";
$query->query_vars[ 'page' ] = "";
$query->request = "series";
$query->matched_query = "pagename=series&page=";
}
//See what is returned.
echo "<br><br><p>Parse Request</p>";
var_dump($query);
return $query;
}
Trying to get some insight into why this doesn't work, solutions to create the result I want, and perhaps some other ways you can use 'parse_request'.
Share Improve this question edited Jan 5, 2018 at 23:15 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Jan 5, 2018 at 23:03 Ameer BacchusAmeer Bacchus 632 silver badges5 bronze badges 1- your code works as-is for me, it seems your issue is elsewhere. – Milo Commented Jan 5, 2018 at 23:44
1 Answer
Reset to default 2I would just add a rewrite rule rather than try to modify query parsing.
function wpd_series_query_var( $vars ){
$vars[] = 'wpd_series';
return $vars;
}
add_filter( 'query_vars', 'wpd_series_query_var' );
function wpd_series_rewrite_rule() {
add_rewrite_rule(
'^series/([^/]+)/?$',
'index.php?pagename=series&wpd_series=$matches[1]',
'top'
);
}
add_action( 'init', 'wpd_series_rewrite_rule' );
Don't forget to flush_rewrite_rules() after adding new ones.
You can then get wpd_series
anywhere after the parse_query
action with get_query_var('wpd_series')
.
本文标签: urlsAdding a hook to 39parserequest39 so that siteurlpagenamexyz ignores quotxyzquot
版权声明:本文标题:urls - Adding a hook to 'parse_request' so that siteurlpagenamexyz ignores "xyz" 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742220820a2435423.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论