Inloggen
Aan de slag

Easy Digital Downloads Documentatie

Documentatie, Referentiemateriaal en Tutorials voor Easy Digital Downloads 

Software Licensing – Activeren, Controleren en Deactiveren van Licentiesleutels in WordPress Plugins

Belangrijke opmerking

Deze documentatie is niet nodig als u de Software Licensing API Updater gebruikt. Raadpleeg dit document alleen als u de updater niet gebruikt of als dit ter referentie is:
Bekijk onze documentatie over hoe u automatische upgrades voor uw WordPress  plugins integreert.
Bekijk onze documentatie over hoe u automatische upgrades voor uw WordPress thema's integreert.

Activeren

Met Software Licensing moet een licentiesleutel geactiveerd zijn om volledig te kunnen worden gebruikt. Dit kan op een van de volgende twee manieren:

  1. Een sitebeheerder kan handmatig op de link "Activeren" voor de licentie klikken op de pagina Downloads → Licenties
  2. De koper kan de licentie op afstand activeren via een systeem in uw plugin/thema/software dat de API gebruikt om de activering te starten

Opmerking: de voorbeeldplugin en de implementatie die in dit document worden getoond, zijn puur voorbeelden. De beste implementaties van Software Licensing nemen deze voorbeelden en integreren ze naadloos in de bestaande instellingenpagina van uw plugin.

In deze documentatie laten we zien hoe u een licentie op afstand activeert via een systeem dat aan uw WordPress plugin is toegevoegd. De voorbeeldcode die we in dit voorbeeld laten zien, is exact dezelfde code die is opgenomen in de voorbeeldplugin en thema's, beschikbaar voor download na aankoop van de extensie.

Er zijn twee hoofdonderdelen voor het op afstand activeren van een licentie:

  1. Het opslaan van de licentiesleutel in de gegevens (een thema- of pluginoptie)
  2. Het verzenden van de opgeslagen licentiesleutel via de API naar de winkelwebsite voor verificatie en activering

Wanneer een licentie op afstand wordt geactiveerd, wordt de status van de licentie in het dashboard van uw site bijgewerkt van "inactief" (de standaardstatus) naar "actief".

Laten we eerst kijken naar het maken van een eenvoudige optiepagina om onze licentie op te slaan.

Deze code stelt een sub-menupunt in het menu Plugins in, genaamd "Plugin Licentie". We gebruiken in dit voorbeeld een pluginpagina, maar de code is identiek voor thema's.

Er zijn twee regels bovenaan de code:

De eerste is de licentie zelf en de tweede is de status van de licentie. Zodra we onze licentie hebben geactiveerd, veranderen we de status (op onze lokale site) naar "actief". Dit is zodat we een knop "Licentie Activeren" kunnen tonen als de licentie nog niet is geactiveerd, en de knop kunnen verbergen als dat wel het geval is. Zie de onderstaande schermafbeelding:

Het idee hier is dat we eerst een licentiesleutel invoeren en op "Wijzigingen opslaan" klikken, waardoor de licentiesleutel wordt opgeslagen in onze plugin/thema-opties. Zodra de optie is opgeslagen, klikken we op de knop "Licentie Activeren" om de API-aanroep te starten.

De activeringsknop is slechts een eenvoudig invoerveld met een type "submit" en een naamattribuut dat verschilt van onze opslagknop. De namen *moeten* verschillend zijn, zodat we kunnen weten wanneer op de knop "Licentie Activeren" is geklikt.

In order to activate the license, we just need to “watch” for when the activate button is clicked. The way that we do this is by setting up a function that is hooked to the “admin_init” hook, like so:

If everything runs okay after clicking the “Activate License” button, the activate button will be replaced with the word “active”, and the license status will reflect the newly activated state in your EDD store’s dashboard. If there is an error when activating the license key, the page will be reloaded and an error and message parameter will be added to the URL. We can then use the admin_notices hook (or any other applicable method) to display the error to the customer:

For example, if a customer enters an invalid license key, an error message will be displayed:

Controleren

With Software Licensing you can easily check if a license key is valid at any time. You may want to do this in order to limit certain functionality in the theme or plugin to only users with a valid license key.

Checking a license key’s validity is quite simple; all it requires is that we perform a remote request to our store website with a couple of specific parameters. See the function below:

function edd_sample_theme_check_license() {
$store_url = 'https://yoursite.com';
$item_name = 'Your Item Name';
$license = '834bbb2d27c02eb1ac11f4ce6ffa20bb';
$api_params = array(
'edd_action' => 'check_license',
'license' => $license,
'item_name' => urlencode( $item_name ),
'url' => home_url(),
'environment' => function_exists( 'wp_get_environment_type' ) ? wp_get_environment_type() : 'production',

);
$response = wp_remote_post( $store_url, array( 'body' => $api_params, 'timeout' => 15, 'sslverify' => true ) );
if ( is_wp_error( $response ) ) {
return false;
}

$license_data = json_decode( wp_remote_retrieve_body( $response ) );

if( $license_data->license == 'valid' ) {
echo 'valid';
exit;
// this license is still valid
} else {
echo 'invalid';
exit;
// this license is no longer valid
}
}

Note: if you are consistently checking the validity of a license key, perhaps to block usage of the plugin or theme when the license expires, you MUST cache the results of the check so that the request is not performed with every page load. The Transients API is a great way to cache API responses.

Deactiveren

With Software Licensing, after a license has been activated, it can also be deactivated remotely (since v1.3). Usually, the reason to deactivate a license is to permit a license to be activated on an additional site once the activation limit has been reached.

The deactivation process is nearly identical to the activation process. We perform a remote request and send the details of the license key we are deactivating. The remote request is done like so:

// data to send in our API request
$api_params = array(
'edd_action' => 'deactivate_license',
'license' => '2ec66bae356be570236531ccba06a45b',
'item_name' => 'Sample Plugin', // the name of our product in EDD
'url' => home_url(),
'environment' => function_exists( 'wp_get_environment_type' ) ? wp_get_environment_type() : 'production',
);
// Send the remote request
$response = wp_remote_post( 'https://yoursite.com', array( 'body' => $api_params, 'timeout' => 15, 'sslverify' => true ) );

The $response var will be a json object that tells us whether the deactivation request was successful or not. If it is successful, the response will be:

Note: The date/time is a timestamp unlike other json responses which will be date time.

{
	"success": true,
	"license": "deactivated",
	"item_name": "Sample Plugin",
	"expires": 1556150399,
	"payment_id": 2444,
	"customer_name": "John Doe",
	"customer_email": "[email protected]"
}

If the license fails to be deactivated, the response will be:

{
	"success": false,
	"license": "failed",
	"item_name": "Sample Plugin",
	"expires": 1556150399,
	"payment_id": 2444,
	"customer_name": "John Doe",
	"customer_email": "[email protected]"
}

Once a license has been successfully deactivated, the Site Count column in Downloads → Licenses will be decremented and an entry will be recorded in the log so that admins can see where a license was deactivated from.

Was dit artikel nuttig?

Begin vandaag nog met verkopen!

Sluit u aan bij meer dan 50.000 slimme winkel eigenaren, en begin met de eenvoudigste manier om digitale producten te verkopen met WordPress.

Copyright © 2025 Sandhills Development, LLC

[universally_switcher]