Split (A/B) testing and the Theme Wrapper

First, I know what I am asking for is not really A/B testing… it was just the nearest concept I could think of for the subject.

I’m not entirely sure how to phrase this question but I am looking for a way to display different home pages given different URIs. I have been asked to mock-up a few different static homepage designs for review by our team and would like to just be able to send out urls in the form “/hometest-a”, “/hometest-b”, etc. So people can see live examples of different layouts. I know how this can be done with template_redirect or template_include in themes that do not leverage the theme wrapper, but I am looking for a consistent way to implement this in Sage. This is something I am frequently tasked with and would like an easy & repeatable way to simple drop in a new template (homepage-a.php) and match it up to a URI ("/hometest-a").

I am sure there are a few approaches to this and I am just trying to get some feedback or ideas that will work smoothly with Sage.

Here is what I have currently which works, but the page title comes back “Page not found”:

function template_include_callback($original) {
    $target = $_SERVER['REQUEST_URI'];
    $target = str_replace('/', '', $target);

    if(strcasecmp($target, 'homepage-a') == 0) {
        return 'homepage-a.php';
    } else {
      return $original;
    }
}
add_filter('template_include', 'template_include_callback');
1 Like

I think the easiest way would be to use two separate Pages, each with its own custom Page Template. With that method there really should be no need to dive into the Filter/Action API and redirects or 404s shouldn’t be an issue at all.

Yea that is a solid solution. I guess I was leaning this way because you then could leverage this into real A/B testing by including logic in the template_include filter to load different pages randomly. This could be done without the URI checking, just check for is_front_page and serve up A/B homepage depending on a coin flip. I know there are plugins and products to assist with A/B testing Wordpress, but we have a pretty limited use case and that all seems overboard for us. I actually have not even been asked to implement anything like that yet, but I know it will follow once our team realizes that we need to support these homepage change decisions with analytics data.

Thanks for the suggestion!

You need to filter template_include before the wrapper does, so set the priority below 99. Here’s an example that worked for me:


function alt_templates($template) {
  if (isset($_GET['template'])) {
    if ($_GET['template'] === 'alt' && is_front_page()) {
      return 'front-page-alt.php';
    }
  }

  return $template;
}
add_filter('template_include',  __NAMESPACE__ . '\\alt_templates', 98);
1 Like

I am using Nelio A/B Testing and needed to add the following into the wrap function in lib/wrapper.php

if ( ! empty( get_post_meta( get_the_ID(), '_is_nelioab_alternative', true ) ) ) {
	if ( is_page() ) {
		$main = get_page_template();
	}//end if
}//end if

You could ( possibly should ) add this using a filter. Hope this helps anyone who runs into the same issue.