1. Home
  2. Documentation
  3. Selling with Freemius
  4. Handling Licensing

Handling Licensing

It’s quite easy to be able to have your product support a licensing mechanism with Freemius. All you need to do is add some logic that will help Freemius understand which parts (code blocks) of your product belong to which of your payment plans. Once you have that logic in place Freemius will automatically use it to determine which of your product’s capabilities should be included or excluded on each plan.

Here are some concrete examples to help clarify this better:

Note that in the following examples the product’s shortcode is referred to as my_fs and the paid plan’s name is professional. It will obviously be different for you when you come to customize it.

To check if a website is eligible to use any of the premium defined logic (if the user is either currently in a trial period or simply owns a valid license), simply use:

// This IF block will be auto removed from the Free version.
if ( my_fs()->is__premium_only() ) {
    // This IF will be executed only if the user in a trial mode or have a valid license.
    if ( my_fs()->can_use_premium_code() ) {
        // ... premium only logic ...
    }
}

Here’s how you can add customized marketing content to encourage your users to upgrade from your free to your your paid version:

if ( my_fs()->is_not_paying() ) {
    echo '<section><h1>' . __('Awesome Professional Features', 'my-text-domain') . '</h1>';
    echo '<a href="' . my_fs()->get_upgrade_url() . '">' . __('Upgrade Now!', 'my-text-domain') . '</a>';
    echo '</section>';
}

Here’s an easy way for you to add logic which will only be available in your premium plugin version:

// This "if" block will be auto removed from the Free version.
if ( my_fs()->is__premium_only() ) {
    // ... premium only logic ...
}

To add a function which will only be available in your premium plugin version, simply add premium_only as a suffix to the function’s name. Make sure that all lines that call that method either directly, or by using hooks, are also wrapped in the premium only logic:

class My_Plugin {
    function init() {
        ...

        // This "if" block will be auto removed from the free version.
        if ( my_fs()->is__premium_only() ) {
            // Init premium version.
            $this->admin_init__premium_only();

            add_action( 'admin_init', array( &$this, 'admin_init_hook__premium_only' );
        }

        ...
    }

    // This method will be only included in the premium version.
    function admin_init__premium_only() {
        ...
    }

    // This method will be only included in the premium version.
    function admin_init_hook__premium_only() {
        ...
    }
}

Here’s how you can add logic which will only be executed for customers on one of your paid plans. Simply switch the string professional with whatever name you gave the plan you wish to apply this logic to:

if ( my_fs()->is_plan('professional', true) ) {
    // .. logic related to professional plan only ...
}

Add logic which will only be executed for customers on a specific plan you choose, OR any higher tier. Simply switch the string professional with whatever name you gave your plan:

if ( my_fs()->is_plan('professional') ) {
    // ... logic related to professional plan and higher plans ...
}

Add logic which will only be executed for customers on a specific plan you choose, AND any higher tier. Simply switch the string professional with whatever name you gave your plan:

// This "if" block will be auto removed from the Free version.
if ( my_fs()->is_plan__premium_only('professional') ) {
    // ... logic related to professional plan and higher plans ...
}

Add logic only for users in a trial period:

if ( my_fs()->is_trial() ) {
    // ... logic for users in trial ...
}

Add logic for any specified paid plan. Simply switch the word professional with whatever name you gave your plan:

// This "if" block will be auto removed from the Free version.
if ( my_fs()->is__premium_only() ) {
    if ( my_fs()->is_plan( 'professional', true ) ) {

        // ... logic related to professional plan only ...

    } else if ( my_fs()->is_plan( 'business' ) ) {

        // ... logic related to Business plan and higher plans ...

    }
}

Excluding files and folders from the free plugin version:

There are two ways to exclude files from your free version:

  1. Add premium_only just before the file extension. For example: functions__premium_only.php will be only included in the premium plugin version. This works for all type of files, not only PHP.

  2. Add @fs_premium_only, a special meta tag, to the plugin’s main PHP file header. Example:

    /**
     * Plugin Name: My Very Awesome Plugin
     * Plugin URI:  http://my-awesome-plugin.com
     * Description: Create and manage Awesomeness right in WordPress.
     * Version:     1.0.0
     * Author:      Awesomattic
     * Author URI:  http://my-awesome-plugin.com/me/
     * License:     GPLv2
     * Text Domain: myplugin
     * Domain Path: /langs
     *
     * @fs_premium_only /lib/functions.php, /premium-files/
     */

    if ( ! defined( 'ABSPATH' ) ) {
        exit;
    }

    // ... my code ...

The file /lib/functions.php and the directory /premium-files/ will be removed from the free plugin version.

WordPress.org Compliance

FInally, regarding WordPress.org Compliance:

Based on WordPress.org Guidelines you are not allowed to submit a plugin that has premium code in it:

“All code hosted by WordPress.org servers must be free and fully-functional. If you want to sell advanced features for a plugin (such as a “pro” version), then you must sell and serve that code from your own site, we will not host it on our servers.”

Therefore, if you want to deploy your free plugin’s version to WordPress.org, make sure you wrap all your premium code with an if statement like so:

if ( my_fs()->is__premium_only() ) {
    // ... my premium only logic ...
}

or use the other methods provided to exclude premium features & files from the free version.