All posts

How to set up license keys in a WordPress plugin

Nicholas Affonso

January 10, 2026

How to set up license keys in a WordPress plugin

Selling a premium WordPress plugin requires a way to verify that users have a valid license before granting access to your plugin's features. In this guide, you'll add license key activation and validation to a WordPress plugin using PHP.

You'll also see how to connect a payment provider so licenses are delivered automatically after purchase.

Before you start

Make sure you've created the basics inside your Keyforge account:

Activating a license

When a user enters their license key in your plugin's settings page, send it to Keyforge to activate it for their site. Use home_url() as the device identifier to tie the license to the specific WordPress installation.

$response = wp_remote_post(
    'https://keyforge.dev/api/v1/public/licenses/activate',
    [
        'headers' => ['Content-Type' => 'application/json'],
        'body'    => json_encode([
            'licenseKey'       => 'ABCDE-ABCDE-ABCDE-ABCDE-ABCDE',
            'deviceIdentifier' => home_url(),
            'deviceName'       => get_bloginfo('name'),
            'productId'        => 'p_123456',
        ]),
    ]
);

$data = json_decode(wp_remote_retrieve_body($response), true);
update_option('my_plugin_license_key', $data['license']['key']);

After activation, the license key is saved as a WordPress option so it can be retrieved on future requests.

Per-site activation

Using home_url() as the device identifier means each WordPress site uses one activation slot. You control the maximum number of sites allowed per license from the dashboard.

Validating a license

Validate the stored license key when your plugin loads to ensure it's still active. A good place to do this is in your plugin's main file or an initialization hook.

$license_key = get_option('my_plugin_license_key');

$response = wp_remote_post(
    'https://keyforge.dev/api/v1/public/licenses/validate',
    [
        'headers' => ['Content-Type' => 'application/json'],
        'body'    => json_encode([
            'licenseKey'       => $license_key,
            'deviceIdentifier' => home_url(),
            'productId'        => 'p_123456',
        ]),
    ]
);

$data = json_decode(wp_remote_retrieve_body($response), true);

if ($data['isValid']) {
    // Enable plugin features
} else {
    // Show a license prompt in the settings page
}

To avoid a network request on every page load, cache the result in a transient and re-validate every few hours.


Payments and customer portal

Connect a payment provider to automatically generate and deliver a license key by email when someone purchases your plugin, with no backend or webhooks required. Keyforge supports Stripe, Lemon Squeezy, and Polar for both one-time purchases and subscriptions.

For subscriptions, license expiration dates update automatically on each renewal, and the license is deactivated if a subscription is cancelled. Follow the payments setup guide to connect your product. Customers can manage their licenses, sites, and billing through the customer portal.

License upgrades and renewals

You can offer customers the option to upgrade their license to a higher tier or extend the expiration date of a timed license, directly from the customer portal with no code required. Configure up to 3 upgrade or renewal options per product in the dashboard.

Perpetual fallback

Perpetual fallback lets customers keep using your plugin with limited functionality after their license expires, rather than losing access entirely. When enabled on a product, the validate endpoint returns status: "fallbacked" for expired licenses. Note that isValid remains true in this case, so check the status field to differentiate.

if ($data['status'] === 'active') {
    // Full access
} elseif ($data['status'] === 'fallbacked') {
    // License expired - grant limited access based on your plugin's logic
} else {
    // Invalid - show a license prompt in the settings page
}

Conclusion

You now have a complete licensing setup for your WordPress plugin. Keyforge handles license management and payment automation so you can focus on building your plugin.

For more details, visit the Keyforge documentation.

Simplify your licensing process

Focus on building your product and let us handle licensing. Manage license keys via payments and offer your customers a smooth self-serve experience.