Explore Gists to customize the Freemius WordPress SDK
Here are some examples of code snippets and gists that demonstrate how to customize the Freemius WordPress SDK for various use cases.
These examples cover a range of customizations. Feel free to explore and adapt these snippets to fit your specific needs when working with the Freemius WordPress SDK.
Add permission for adding Help Scout Beacon
See the permission_list filter hook documentation for more details.
if (
// Checks if user opted-in (or activated a license).
my_fs()->is_registered() &&
// Checks if user didn't opt out from tracking.
my_fs()->is_tracking_allowed()
) {
// Render Help Scout Beacon.
require_once( '../path/to/help-scout-beacon.php' );
}
EDD to Freemius Pricing Page Override based on URLs / Href
<script src="https://checkout.freemius.com/checkout.min.js"></script>
<script>
(function ($) {
$(document).ready(function () {
var handler = FS.Checkout.configure({
plugin_id: '<product_id>',
plan_id: '<plan_id>',
public_key: '<product_public_key>',
});
$('.purchase a').on('click', function (e) {
debugger;
var $this = $(this),
href = $this.attr('href'),
price_id = parseInt(href.substr(-1), 10),
licenses = 1,
billing_cycle = 'annual';
switch (price_id) {
case 2:
licenses = 3;
break;
case 3:
licenses = 25;
break;
case 4:
licenses = 25;
billing_cycle = 'lifetime';
break;
case 1:
default:
licenses = 1;
break;
}
handler.open({
licenses: licenses,
billing_cycle: billing_cycle,
});
e.preventDefault();
return false;
});
});
})(jQuery);
</script>
Freemius Purchase Completion JavaScript Callback Filter
<?php
// Add GA tracking only if user opted-in OR if non-WP.org compliant product.
function my_after_purchase_js( $js_function ) {
return 'function ( response ) {
/**
* Since the user just entered their personal & billing information, agreed to the TOS & privacy,
* know they are running within a secure iframe from an external domain, they implicitly permit tracking
* this purchase. So initizlizing GA here (after the purchase), is legitimate.
*/
ga('create', 'UA-XXXXXXX', 'auto');
console.log("checkout", "purchaseCompleted");
}';
}
function my_checkout_enrich ( $html ) {
return '<script type="text/javascript">
(function() {
if ( null == ga ) {
// Add code to include GA.
}
})();
</script>' . $html;
}
my_fs()->add_filter('checkout/purchaseCompleted', 'my_after_purchase_js');
my_fs()->add_filter('templates/checkout.php', 'my_checkout_enrich');
Freemius Buy Button Code for a Multi-Plans Table
<script src="https://checkout.freemius.com/checkout.min.js"></script>
<select id="starter-licenses">
<option value="1" selected="selected">Single Site License</option>
<option value="5">5-Site License</option>
<option value="25">25-Site License</option>
<option value="unlimited">Unlimited Sites License</option>
</select>
<button id="starter-purchase">Buy Starter</button>
<select id="pro-licenses">
<option value="1" selected="selected">Single Site License</option>
<option value="5">5-Site License</option>
<option value="25">25-Site License</option>
<option value="unlimited">Unlimited Sites License</option>
</select>
<button id="pro-purchase">Buy Professional</button>
<select id="bus-licenses">
<option value="1" selected="selected">Single Site License</option>
<option value="5">5-Site License</option>
<option value="25">25-Site License</option>
<option value="unlimited">Unlimited Sites License</option>
</select>
<button id="bus-purchase">Buy Professional</button>
<script type="text/javascript">
(function () {
var freemiusHandler = FS.Checkout.configure({
name: '<pluginTitle>',
plugin_id: '<pluginID>',
public_key: '<publicKey>',
});
var plans = {
starter: '<starterPlanID>',
pro: '<proPlanID>',
bus: '<busPlanID>',
};
var addBuyHandler = function (plan, planID) {
$('#' + plan + '-purchase').on('click', function (e) {
freemiusHandler.open({
plan_id: planID,
licenses: $('#' + plan + '-licenses').val(),
// You can consume the response for after purchase logic.
success: function (response) {
// alert(response.user.email);
},
});
e.preventDefault();
});
};
for (var plan in plans) {
if (!plans.hasOwnProperty(plan)) continue;
addBuyHandler(plan, plans[plan]);
}
})();
</script>