Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example PHP Loop Using Switch and Case
// Load Testimonials script for specific pages
add_action( 'pre_get_posts', 'mv_show_testimonials_script' );
function mv_show_testimonials_script() {
if ( is_page('services') || is_home() || is_front_page() ) {
// Let's set some server vars
if(strpos($_SERVER['REQUEST_URI'], 'staging1') !== false) {
$mv_server = 'stage_one';
} elseif (strpos($_SERVER['REQUEST_URI'], 'staging2') !== false) {
$mv_server = 'stage_two';
} elseif (strpos($_SERVER['REQUEST_URI'], '192.168.1.10') !== false) {
$mv_server = 'local';
} elseif (strpos($_SERVER['REQUEST_URI'], 'mattvaden.com') !== false) {
$mv_server = 'live_tlms';
}
// Now we include the tlms script from the correct dir
switch ($mv_server) {
case "stage_one":
$mv_stage_one_tlms = include_once ('/some/home/dir/staging/1/wp-content/plugins/tmls_testimonials/testimonials.php');
var_dump($mv_stage_one_tlms);
break;
case "stage_two":
$mv_stage_two_tlms = include_once ('/some/home/dir/staging/2/wp-content/plugins/tmls_testimonials/testimonials.php');
var_dump($mv_stage_two_tlms);
break;
case "local":
$mv_local_tlms = include_once ('C:\xampp\htdocs\some\home\dir\wp-content\plugins\tmls_testimonials\testimonials.php');
var_dump(mv_local_tlms);
break;
case "live_tlms":
$mv_live_tlms = include_once ('/some/home/dir/public_html/wp-content/plugins/tmls_testimonials/testimonials.php');
var_dump($mv_live_tlms);
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment