Skip to main content

How to deploy JavaScript SDK to a Wordpress site?

Question

How to deploy JavaScript SDK code in a Wordpress site?

Answer

The steps below explain how to use JavaScript SDK in a blank page within a Wordpress site

  1. First step is to install Header and Footer Scripts plugin. While this is not required, it is a good practice to load the JavaScript SDK library within the page header.

  1. In your Site Development page, create a sample page.

  1. Open the page in Edit mode, scroll to the bottom and paste the import script tag under Insert Script to <Head> section.
<script src="https://cdn.split.io/sdk/split-10.21.1.min.js"></script>

  1. Click on the + sign to insert new block, and choose Custom HTML.

  1. The Custom HTML block allows any HTML elements, including JavaScript, copy and paste the code below inside it, make sure to replace the API KEY with a valid key, set the User key and feature flag name as well.
<p id="sdk"></p>
<script>
factory = splitio({ core: {
authorizationKey: 'SDK API KEY',
key: 'USER KEY',
},
startup: {
readyTimeout: 4, requestTimeoutBeforeReady: 4
},
});
client = this.factory.client();
client.on(client.Event.SDK_READY, () => {
treatment = client.getTreatment("SPLIT NAME");
console.log("treatment = "+treatment);
var pobj = document.getElementById("sdk");
pobj.innerHTML = "<p>Split: [SPLIT NAME], Treatment = "+treatment+"</p>";
});
</script>
  1. Save and review the page, once it loads, the treatment is calculated after the SDK_READY event fires and display the feature flag name and treatment value, in the <p> section.