0

I have 2 pages:

  1. /contact
  2. /contact-team

The /contact page has a popup and on user selection, the page must reload but with the contents of /contact-team.

Is there a filter hook which can load a different post altogether after the URL has been generated?

I have tried the pre_get_posts to set the post ID but it gets redirected to that ID. I want the page loaded to be /contact but the content should be from /contact-team. Any ideas?

4 Answers 4

1

You can use the request hook to change the loaded page for the same URL:

add_filter( 'request', function( $request ){
    //Replace this with custom logic to determine if the user should see the contact-team page
    $load_team_contact = true;

    if( $load_team_contact && isset( $request['pagename'] ) && 'contact' == $request['pagename'] ){
        $request['pagename'] = 'contact-team';
    }
    return $request;
} );

You just need to determine if the user should see the contact team page which would vary depending on your setup.

0

You can try the_content filter something like below to load a content from a different post/page

function get_contact_team_page_content ( $content ) {

    // Condition based on user selection via popup e.g. $_GET or $_POST
    if ('condition') {
        $contact_team = get_post(14); // e.g. contact-team page ID
        $content = $contact_team->post_content;
        return $content;
    }

    return $content;
}

add_filter( 'the_content', 'get_contact_team_page_content');
1
  • This won't load the styles for that page Commented Dec 12, 2020 at 14:58
0

Using a cookie it would be:

function get_contact_team_page_content ( $content ) {
  if (isset($_COOKIE['alternative_content'])) {
    $contact_team = get_post( (int)$_COOKIE['alternative_content'] ); // e.g. contact-team page ID
    unset($_COOKIE['alternative_content']);
    return $contact_team->post_content;
  }
  return $content;
}

add_filter( 'the_content', 'get_contact_team_page_content');

In this way ( adding the code above in functions.php of your theme or plugin) you can filter the_content based on the existence of a cookie

in you page you attach a function to the click button that reloads the page:

<!-- 158 is your contact-team page ID -->
<button onClick="loadAlternateContent(158)">change</button>
<script>
function loadAlternateContent(post_id){
  var date = new Date();
  date.setTime( date.getTime() + (1*60*1000) );  //expiration in 1 minute
  var expires = "; expires=" + date.toGMTString();
  document.cookie = "alternative_content=" + post_id + expires +"; path=/";
  window.location.reload();
}
</script>

you see the content is the post_id 158 content but the url is the same.

At this point, reloading the page manually, the first time will still show the alternative content since the cookie has been deleted by PHP but it's still living in the browser. To avoid this behaviour you should have a function that deletes the cookie on the 'alternative_content' page:

<script>
  window.addEventListener("load", function(event) {
  document.cookie = 'alternative_content=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
});
</script>
0

I'm not sure if I understand well your question but, you can get a post data inside another post this way:

$post   = get_post( 123 ); // Where 123 is the ID
$output =  apply_filters( 'the_content', $post->post_content );

You can also use wp_query if you want more control.

2
  • This won't load the styles for that page Commented Dec 12, 2020 at 14:58
  • @SiddharthThevaril , we are not talking about styles but yes ;) they will not be loaded if they are not inside 'the_content'
    – gtamborero
    Commented Dec 12, 2020 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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