Pro Plugin Sale! Save 25% on all pro plugins w/ code: SEASONS
Web Dev + WordPress + Security

Conditionally Load WordPress Pages

Need to load a WordPress page conditionally? For example, perhaps you need a “special” page to appear for search results? Yes? This simple PHP/JavaScript solution may be just what the doctor ordered!

Simply replace “condition” with the required condition (or delete the if (condition) {} qualifier entirely), and then change the path and file names to suit your specific needs:

<?php
if (condition) {
	
	echo ("
		<script type=\"text/javascript\">
		<!--//--><![CDATA[//><!--
		
		parent.location='http://www.domain.com/path/to/file.html';
		
		//--><!]]>
		</script>
	");
	
} else {
	
	echo ("
		<script type=\"text/javascript\">
		<!--//--><![CDATA[//><!--
		
		parent.location='http://www.domain.com/path/to/other-file.html';
		
		//--><!]]>
		</script>
	");
	
}
?>

Specifically, I employ the following trickery in my Killer WordPress theme:

.
.
.
<?php endwhile; else: ?>
	
	<?php echo ("
		<script type=\"text/javascript\">
		<!--//--><![CDATA[//><!--
		
		parent.location='http://example.com/subdirectory/?p=58';
		
		//--><!]]>
		</script>
	"); ?>
	
<?php endif; ?>

The previous code chunk returns a specially designed search results page that is specifically tailored to suit the Killer theme. Works wonders.

Jeff Starr
About the Author
Jeff Starr = Designer. Developer. Producer. Writer. Editor. Etc.
The Tao of WordPress: Master the art of WordPress.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
USP Pro: Unlimited front-end forms for user-submitted posts and more.
Thoughts
BF Sale! Save 40% on all Pro WordPress plugins and books w/ code FRIDAY23
Sincerely trying to engage more on social media. I love the people not the platforms.
All free and pro WordPress plugins updated and ready for WP version 6.4!
Fall season almost here :)
My greatest skill on social media is the ability to simply ignore 98% and keep scrolling without interacting.
Enjoying this summer, getting some great positive energy. Refreshing and inspiring.
☀️ Pro plugin giveaway! Enter to win 1 of 4 lifetime licenses for our WordPress security plugins, including 10-site Security Bundle!
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.