This repository contains an example WordPress plugin that demonstrates how to implement automatic updates for premium WordPress plugins using the Lemon Squeezy WordPress plugin and the Lemon Squeezy Licenses API.
- Install the Lemon Squeezy WordPress plugin on your WordPress site and connect it to Lemon Squeezy.
- Make sure external requests to the WordPress REST API (
http://example.com/wp-json
) aren't disabled or blocked by a firewall.
- Copy the
includes/class-plugin-updater.php
file into your plugin. - Rename the
ExamplePluginUpdater
class so that it's unique to your plugin (e.g.MyPluginUpdater
) - Override the
get_license_key()
method to retrieve the users Lemon Squeezy license key. Normally, your plugin would have a settings page where you ask for and store a license key.
- In your plugin code, store the plugin version (e.g. in a
define( 'EXAMPLE_PLUGIN_VERSION', '1.0.0' )
). - In your plugin code, store the API URL for the updater. This should point to your WordPress site that is running the Lemon Squeezy WordPress plugin (e.g.
define( 'EXAMPLE_PLUGIN_API_URL', 'http://example.com/wp-json/lsq/v1' )
) - Instanciate your updater class, passing in the plugin ID and plugin slug as well as the plugin version and API URL.
See example-plugin.php
for an example of how to set this up.
- This plugin code overrides WordPress default updater process to request update information from your WordPress site running the Lemon Squeezy WordPress plugin instead of WordPress.org.
- The Lemon Squeezy WordPress plugin then uses the Lemon Squeezy API to validate the license key and fetch the required file information.
- Finally, the Lemon Squeezy WordPress plugin returns the information required for this plugin to determine if it needs an update or not. If an update is required, the download URL returned by the Lemon Squeezy API will be used to provide the update.
┌─────────────────────────┐ ┌───────────────────────────────────┐ ┌─────────────────────┐
│ │ │ │ │ │
│ Your WordPress Plugin │ │ Lemon Squeezy WordPress Updater │ │ Lemon Squeezy API │
│ │ │ │ │ │
└───────────┬─────────────┘ └─────────────────┬─────────────────┘ └──────────┬──────────┘
│ │ │
│ │ │
│ │ │
│ │ │
│ │ │
│ │ │
│ Request udpate information │ │
├─────────────────────────────────────────────────►│ │
│ │ │
│ │ │
│ │ Request file information │
│ ├───────────────────────────────────────────────►│
│ │ │
│ │ │
│ │ │
│ │ │
│ │ Validate license key │
│ │ + │
│ │ Return file information │
│ │◄───────────────────────────────────────────────┤
│ │ │
│ │ │
│ │ │
│ │ │
│ Return update information │ │
│◄─────────────────────────────────────────────────┤ │
│ │ │
│ │ │
│ │ │
│ │ │
│ │ │