I have 2 pages:
/contact/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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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.
Method 2
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');
Method 3
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>
Method 4
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0