0

I have a created a stuff page in WP.

When a user visits stuff/foo, I'm want to fetch some data from an API append it to the contents of the stuff page.

So far I've tried:

  • hooking into the_content filter but it's not working because WP already decides that the page doesn't exist and returns a 404.
  • using init_query_var and reset the query inside to $query->set('pagename', 'stuff'). However, this forces WP to redirect to the /stuff page.

Thanks for looking

  • You'll probably want to look into rewrite rules to make that possible. – janh Feb 3 '18 at 19:09
1

I belive this will work for you:

Create a "page" called "stuff", then create another page called "foo", make it child of "stuff", now stuff/foo will exist and won't be 404.

Finally use the_content filter and make the required data manipulation.

The other choice (hard way) you'd have is to use pre_get_post hook, so you can make wordpress believe it's really "foo" page that's being requested, and avoid 404.

0

One way that you could do this is to use a short code that would depend on the parameter passed through.

For example, use

https://codex.wordpress.org/WordPress_Query_Vars

to allow the parameter to be passed into the page. ie:

function add_query_vars_filter( $vars ){
  $vars[] = "query";
  return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );

Rewrite Rules can be used to remove the query string and create a fake page foo/bar

You can then create a short code that would pull the data back from the api based on this query var.

for example: [display-content] might use the query string baz to pull back content from an api. baz would be set via a Rewrite Rule which would mask the ?baz= element off of the url.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.