Skip to content

Instantly share code, notes, and snippets.

@douglaskarr
Last active February 14, 2018 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglaskarr/fd715da1db15f65195e3f6fb40759e42 to your computer and use it in GitHub Desktop.
Save douglaskarr/fd715da1db15f65195e3f6fb40759e42 to your computer and use it in GitHub Desktop.
WordPress: Add A/B Testing Functionality to Gravity Forms
<?php
// Add to your functions.php theme file
// Modified slightly from Mediacause so all you need to do is add _ab to your shortcode and then specify your forms
// Credit: https://mediacause.org/ab-testing-forms-using-gravity-forms/
function shortcode_gravityforms_ab($atts){
$a = shortcode_atts( array(
'id' => '',
'title' => 'false',
'description' => 'false',
'ajax' => 'false'
), $atts);
$ids = explode(",", $a['id']);
$count = count($ids) - 1;
if($count > 0){
$rand = mt_rand(0,$count);
return do_shortcode('[gravityform id="'. $ids[$rand].'" title="'. $a['title']. '" description="'. $a['description'] .'" ajax="'. $a['ajax'] .'"]');
}
}
add_shortcode('gravityform_ab', 'shortcode_gravityforms_ab');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment