Book Sale! Code WP2025 takes 20% OFF our Pro Plugins & Books »
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'>
			parent.location='http://www.domain.com/path/to/file.html';
		</script>
	");
	
} else {
	
	echo ("
		<script type='text/javascript'>
			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'>
			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.

About the Author
Jeff Starr = Web Developer. Security Specialist. WordPress Buff.
Wizard’s SQL for WordPress: Over 300+ recipes! Check the Demo »
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 »
SAC Pro: Unlimited chats.
Thoughts
Finishing up the pro version of Head Meta Data plugin, launch planned this month.
Finally finished my ultimate block list to stop AI bots :) Blocks over 100 AI bots!
After 10 years working late at night, my schedule has changed. I am now a “morning person”, starting my day at 6am or earlier.
Nice update for Wutsearch search engine launchpad. Now with 19 engines including Luxxle AI-powered search.
New version of 8G Firewall (v1.4) now available for download :)
Wishing everyone a prosperous and bright New Year!
I disabled AI in Google search results. It was making me lazy.
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.