page template - How to display the contents of URL1 when user visits URL2

admin2025-06-02  4

I know how to do it with apache/mod_proxy, but I can't use mod_proxy on our shared host. So no .htaccess solutions.

Currently I have the following code in child theme's page.php

global $post ;
if( needs_replacement( $post ) ) {
  $post_name = $post->post_name ;
  $from_url = create_url( $post );
  $r = wp_remote_get( $from_url);
  echo wp_remote_retrieve_body( $r );
  exit(0);
}
require_once( get_template_directory() . '/page.php' );
?>

And it works. I am wondering what is the right way of doing this. $from_url is from the same server.

Example:

$post = 
$from_url = /?sec=hello-world

Making it clear, what I want to develop is a method that can get the rendered page content when I pass a URL. So something like:

function wp_get_url_content( $url ) {
  ....
}

The $url will always be a URL from the same server.

I know how to do it with apache/mod_proxy, but I can't use mod_proxy on our shared host. So no .htaccess solutions.

Currently I have the following code in child theme's page.php

global $post ;
if( needs_replacement( $post ) ) {
  $post_name = $post->post_name ;
  $from_url = create_url( $post );
  $r = wp_remote_get( $from_url);
  echo wp_remote_retrieve_body( $r );
  exit(0);
}
require_once( get_template_directory() . '/page.php' );
?>

And it works. I am wondering what is the right way of doing this. $from_url is from the same server.

Example:

$post = https://example/docs/hello-world
$from_url = https://example/supp/?sec=hello-world

Making it clear, what I want to develop is a method that can get the rendered page content when I pass a URL. So something like:

function wp_get_url_content( $url ) {
  ....
}

The $url will always be a URL from the same server.

Share Improve this question edited Feb 25, 2019 at 7:17 Dakshinamurthy Karra asked Feb 22, 2019 at 14:41 Dakshinamurthy KarraDakshinamurthy Karra 5463 silver badges9 bronze badges 2
  • Do you mean the referrer? It's not 100% clear what you mean or why you're trying to do this, a little context as to the problem you're trying to solve would help us understand. Additionally, what do needs_replacement and create_url do and how do they work? – Tom J Nowell Commented Feb 22, 2019 at 15:21
  • Basically I have a plugin that provides a short code. When added to a page that creates a menu structure and the URLs of the format https://example/supp/?sec=hello-world from URL https://example/docs/hello-world. the URL https://example/docs/hello-world is a wordpress page. If I open it it shows me the page contents but without the additional menus provided by https://example/supp/?sec=hello-world. So whenever a user goes to https://example/docs/hello-world I want to show the content he gets when using https://example/supp/?sec=hello-world. – Dakshinamurthy Karra Commented Feb 22, 2019 at 16:16
Add a comment  | 

2 Answers 2

Reset to default 0

Have WordPress (which creates the web server responses) respond with a redirect (via a call to wp_redirect()). Put as the first thing in header.php

global $post;
if( needs_replacement( $post ) ) {
  $from_url = create_url( $post );
  wp_redirect( $from_url );
  exit(0);
}
require_once( get_template_directory() . '/page.php' );
?>

(Note that you have to this first thing; otherwise, attempts to redirect will get ignored or more likely cause errors.)

(Thought of another approach)
Use a WordPress hook, and a call to set_query_var().
Something like

function add_my_query_vars () {
   set_query_var('sec', 'hello world'); 
}

add_action('pre_get_posts','add_my_query_vars');

(This does make some likely assumptions about how the plugin handles query variables.)

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748870150a314403.html

最新回复(0)