Changing page template based on URL query

Hello!

I am using the Pro theme, and we have developed special web pages within our Wordpress environment for use in our Mobile App, some of which duplicate the content of the main pages, but with a no header/footer template. When these pages are updated, both the main page and the app page.

We have been trying to find a solution to make it so that we can pull the main page, but add a query to the end of the URL to get it to change the template. I have found some code that works, but unfortunately it doesn’t work on the Pro theme. It firstly displays the page with the correct theme, but then under it, it’s basically repeating the content but with the main template.
Please see here: https://www.gateway978.com/coronavirus?template=page_noheaderfooter

This is the code I am using in the Child theme’s functions.php:

add_filter( 'template_include', 'change_page_template_call' );
function change_page_template_call( $template ) {
	
	// Check if our GET parameter exists and holds a specific value
	if( isset( $_GET['template'] ) && 'page_noheaderfooter' == $_GET['template'] ) {
		
		// Attempt to locate our template
		$new_template = locate_template( array( 'template-blank-3.php' ), true );
		
		return $new_template;
		
	}else{
	    return $template;
	}
	
}

Can anyone help with this please? Thank you!!

Hi Simon,

Thanks for reaching out.
I have tried the following code by changing the template to template-blank-4.php and it seems working. But as it is a filter hook that has been overridden, the body class is still showing the previous template classes, which might misguide.

add_filter( 'template_include', 'wpdocs_include_template_files_on_page' ); 
function wpdocs_include_template_files_on_page( $template ) 
{    
    if( isset( $_GET['template'] ) && 'page_noheaderfooter' == $_GET['template'] ) 
    {
        $new_template = locate_template( array( 'template-blank-4.php' ) );
        if ( !empty( $new_template ) ) 
        {
            return $new_template;
        }     
    }
    return $template;
}

And I have tried your specified page along with and without the template query string and found that your code is working too. Please let us know if you still facing the same issue again.

Thanks

Hi,
Thank you for your reply.
Unfortunately, both my code and your code do not work.

My code - It produces duplicated content of the page. Firstly without header/footer, then if you scroll half-way down the page, you will see it shows the content again but with the header and footer around it.

Your code - Nothing actually changes. The header and footer stay in place regardless. The content shifts around a little bit depending on the template file used, but the actual headers and footers stay in place, and removing these are what I need to achieve.

Please advise. I can supply you with login details to take a look if you need. I have reverted back to my code for the moment and hopefully on the URL above you will be able to see duplicated content. I have tried this in most modern browsers.

Thank you.

Hi Simon,

I would like to request you provide login credentials for your site in a secure note to examine it further, including:

– WordPress Site URL & Login URL
– WordPress Admin username/password
– FTP/CPanel credentials on any case we need to change the file.

To create a secure note, click the key icon underneath any of your posts.

Thanks

Hello, Thank you I have done this now.
Thanks, Simon

Hello Simon,

You may add a priority to your code so that it will be executed after all other template changes in the theme.
add_filter( 'template_include', 'change_page_template_call', 50 );

Regretfully, this particular customization request is outside the scope of our support as this is not related to an issue with the theme and instead has to do with your customization of it. As such, you will need to investigate this particular issue on your own or seek help from a developer should you not feel comfortable making these changes yourself. If you have any further questions about the theme, we are more than happy to provide you with assistance on these inquiries.

Thank you for your understanding.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.