Skip to content

Instantly share code, notes, and snippets.

@pH-7
Last active July 15, 2020 22:23
Show Gist options
  • Save pH-7/454a6dfefac5c32bf4d5 to your computer and use it in GitHub Desktop.
Save pH-7/454a6dfefac5c32bf4d5 to your computer and use it in GitHub Desktop.
Split Testing (marketing A/B testing) with Wordpress - Display a Random Page with Wordpress. Frensh Tutorial πŸ‘‰ https://01script.com/page-aleatoire-wordpress-split-testing/
<?php
/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* @package WordPress
*/
/**
* Tells WordPress to load the WordPress theme and output it.
*
* @var bool
*/
define('WP_USE_THEMES', true);
$aDaysList = ['Tue', 'Wes', 'Fry', 'Sun']; // EX: On Tuesday, Wednesday, Friday and Sunday, redirect to "squeeze-page2", otherwise leave it on the usual squeeze page
$sOriginalSalesPage = 'squeeze-page1'; // Edit it to your Original Sale Page, the Wordpress Page Name
$sAlternativeSalesPage = 'squeeze-page2'; // Edit it to your Alternative Sale Page, the Wordpress Page Name
set_split_test($sOriginalSalesPage, $sAlternativeSalesPage, $aDaysList); // Run the A/B testing
/** Loads the WordPress Environment and Template */
require(dirname(__file__) . '/wp-blog-header.php');
/**
* @author Pierre-Henry Soria <pierrehenrysoria@gmail.com>
* @copyright (c) 2015-2020, Pierre-Henry Soria. All Rights Reserved.
* @link http://github.com/pH-7/
* @link https://01script.com/page-aleatoire-wordpress-split-testing/
*
* Set a Wordpress A/B Testing Page
*
* @param string $sOriginalSalesPage Original Sale Page; the Wordpress Page Name Only (without slash).
* @param string $sAlternativeSalesPage Alternative Sale Page; the Wordpress Page Name Only (without slash).
* @param array $aDaysList Weekdays when the alternative sale page is displayed. Three first letters (with uppercase)
* e.g., array('Tue') = Display the alternative page on Tuesday.
*
* @return void
*/
function set_split_test($sOriginalSalesPage, $sAlternativeSalesPage, $aDaysList)
{
$sDay = date('D');
$sPath = str_replace('/', '', $_SERVER['REQUEST_URI']);
$sHttp = (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS'] == 'on')) ?
'https://' : 'http://'; // Check SSL compatibility
if (in_array($sDay, $aDaysList) && $sPath === $sOriginalSalesPage) {
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $sHttp . $_SERVER['HTTP_HOST'] . '/' . $sAlternativeSalesPage);
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment