$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>page template - How to display the contents of URL1 when user visits URL2|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin9PV0评论

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.)

Post a comment

comment list (0)

  1. No comments so far