$npx -y skills add mralaminahamed/wp-dev-skills --skill wp-freemiusUse when integrating Freemius SDK into a WordPress plugin for monetisation — SDK bootstrap with fs_dynamic_init, feature gating with can_use_premium_code() / is__premium_only() / is_plan() / is_trial() / is_paying(), license management, free/pro dual-zip build using __premium_onl
| 1 | # Freemius SDK Integration |
| 2 | |
| 3 | > **Model note:** SDK bootstrap and basic feature-gating are pattern-matching (`haiku`). Trialware compliance audit and pricing-plan architecture decisions require careful judgment — use `sonnet` for those. |
| 4 | |
| 5 | Integrate Freemius into a WordPress plugin for commercial distribution: SDK bootstrap, free/pro feature gating, license management, trials, pricing page, and the Freemius dashboard. Freemius handles payments, license keys, update delivery, and analytics. |
| 6 | |
| 7 | ## When to use |
| 8 | |
| 9 | - "Add Freemius to my plugin", "set up free/pro version", "implement license management". |
| 10 | - "Gate premium features behind a license", "add a trial period". |
| 11 | - "Create a pricing page", "set up a Freemius affiliate program". |
| 12 | - "Debug Freemius SDK not loading", "fix opt-in dialog not showing". |
| 13 | - "Configure Freemius for multisite licensing". |
| 14 | |
| 15 | **Not for:** General WooCommerce payment flows — use `wp-woocommerce`. WP.org trialware compliance (Freemius-powered upsells must follow WP.org Guideline 5 — use `wp-org-submission`, which contains `references/trialware-compliance.md`). |
| 16 | |
| 17 | ## Method |
| 18 | |
| 19 | ### 1. Create a Freemius account and app |
| 20 | |
| 21 | 1. Sign up at `https://freemius.com` |
| 22 | 2. Create a new **Plugin** product in the Freemius dashboard |
| 23 | 3. Note down: **Plugin ID**, **Public Key**, **Secret Key** |
| 24 | 4. Configure pricing plans (Free, Pro, etc.) in the dashboard |
| 25 | |
| 26 | ### 2. Install the SDK |
| 27 | |
| 28 | **Via Composer (recommended):** |
| 29 | ```bash |
| 30 | composer require freemius/wordpress-sdk |
| 31 | ``` |
| 32 | |
| 33 | **Manual:** Download from `https://github.com/Freemius/wordpress-sdk` and place in `vendor/freemius/`. |
| 34 | |
| 35 | ### 3. Bootstrap the SDK |
| 36 | |
| 37 | Create `includes/freemius.php` (the Freemius singleton init file): |
| 38 | |
| 39 | ```php |
| 40 | <?php |
| 41 | if ( ! function_exists( 'my_plugin_fs' ) ) { |
| 42 | function my_plugin_fs() { |
| 43 | global $my_plugin_fs; |
| 44 | |
| 45 | if ( ! isset( $my_plugin_fs ) ) { |
| 46 | // Include the Freemius SDK |
| 47 | require_once plugin_dir_path( __FILE__ ) . '../vendor/freemius/wordpress-sdk/start.php'; |
| 48 | |
| 49 | $my_plugin_fs = fs_dynamic_init( [ |
| 50 | 'id' => '12345', // Plugin ID from dashboard |
| 51 | 'slug' => 'my-plugin', // WP.org slug |
| 52 | 'type' => 'plugin', |
| 53 | 'public_key' => 'pk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // Public key |
| 54 | 'is_premium' => false, // true if this IS the premium build |
| 55 | 'has_premium_version' => true, // true if premium version exists |
| 56 | 'has_addons' => false, |
| 57 | 'has_paid_plans' => true, |
| 58 | 'trial' => [ |
| 59 | 'days' => 14, |
| 60 | 'is_require_payment' => false, // true = credit card required |
| 61 | ], |
| 62 | 'menu' => [ |
| 63 | 'slug' => 'my-plugin', |
| 64 | 'contact' => false, |
| 65 | 'support' => false, |
| 66 | ], |
| 67 | ] ); |
| 68 | } |
| 69 | |
| 70 | return $my_plugin_fs; |
| 71 | } |
| 72 | |
| 73 | // Init Freemius |
| 74 | my_plugin_fs(); |
| 75 | |
| 76 | // Hook Freemius after initial plugin setup |
| 77 | do_action( 'my_plugin_fs_loaded' ); |
| 78 | } |
| 79 | ``` |
| 80 | |
| 81 | **Load from main plugin file:** |
| 82 | ```php |
| 83 | // At the top of my-plugin.php, before any premium-gated code |
| 84 | require_once plugin_dir_path( __FILE__ ) . 'includes/freemius.php'; |
| 85 | ``` |
| 86 | |
| 87 | ### 4. Feature gating |
| 88 | |
| 89 | Gate premium features consistently throughout the codebase: |
| 90 | |
| 91 | ```php |
| 92 | // Check if user has an active paid plan (or trial) |
| 93 | if ( my_plugin_fs()->can_use_premium_code() ) { |
| 94 | // Show/run premium feature |
| 95 | require_once plugin_dir_path( __FILE__ ) . 'includes/class-premium-feature.php'; |
| 96 | } |
| 97 | |
| 98 | // Check if the premium co |