Skip to main content
Image alt
Categories
How To...Theme

HOW TO: Test your WordPress code on production like a boss

Should you test your new code on live? Probably not. That doesn’t mean there aren’t times when you need to… Well for just those times, you can setup a basic condition so it only executes when you add an query parameter to the url (ie. https://403page.com/?four03labs).

Here is a super simple method for adding small snippets. You can pop it in your theme’s functions.php file (in which case don’t include the <?php, that’ll already be there), or just add it as a standalone mu-plugin:

<?php

$four03labs = isset($_GET['four03labs']);

if ($four03labs) {
#Add any code you like here
#It will only execute if ?403labs is present in the URI

echo 'IM WORKING!!!'. PHP_EOL;
}

else {}

Testing this, you’ll see that https://403page.com/?four03labs shows the I’M WORKING text at the top of the page, whereas requests without ?four03labs will not. You can test snippets out on your live site without disrupting your visitors.

Here’s another method to include a file from the /wp-content/plugins/ directory:

<?php

$four03labs = isset($_GET['four03labs']);

if ($four03labs) {
#Add any code you like here
#It will only execute if ?403labs is present in the URI

include plugin_dir_path( __FILE__ ) . 'subdir/filename.php';

}

else {}

And here’s how to include a file from the wp-content/themes/ directory:

<?php

$four03labs = isset($_GET['four03labs']);

if ($four03labs) {
#Add any code you like here
#It will only execute if ?403labs is present in the URI

include get_theme_file_path( '/subdir/filename.php' );

}

else {}

If you need to include a file outside of these directories you can do this instead:

<?php

$four03labs = isset($_GET['four03labs']);

if ($four03labs) {
#Add any code you like here
#It will only execute if ?403labs is present in the URI

include(dirname(__FILE__) . "/subdir/filename.php");

}

else {}

Bare in mind, if you write a syntax error outside of the if statement, you’ll still impact your live site – so still make sure to be careful…

Happy coding (on production)!