Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save X-Raym/8f1073cfad43d877f5dab7bbbf515ef1 to your computer and use it in GitHub Desktop.

Select an option

Save X-Raym/8f1073cfad43d877f5dab7bbbf515ef1 to your computer and use it in GitHub Desktop.
Auto-activate license after a product purchase in WordPress Easy Digital Downloads.
<?php
// Auto Activate EDD License if Custom Field is True -- by X-Raym
function edd_auto_activate_activate_license( $license_id, $download_id, $payment_id ) {
// Only specific download get auto activation (custom field)
if ( ! get_field( 'auto-activate', $download_id ) )
return false;
$store_url = get_site_url(); // Multilingual site friendly
$license_key = get_field( '_edd_sl_key', $license_id ); // Get license key from license post ID
// data to send in our API request
$api_params = array(
'edd_action' => 'activate_license',
'license' => $license_key,
'item_id' => $download_id, // the name of our product in EDD
'url' => $store_url // Must have an URL so that license check "license" return "valid".
);
// Call the custom API.
$response = wp_remote_post( $store_url , array(
'timeout' => 15,
'sslverify' => false,
'body' => $api_params
) );
// make sure the response came back okay
if ( is_wp_error( $response ) )
return false;
// decode the license data
//$license_data = json_decode( wp_remote_retrieve_body( $response ) ); // For debugging purpose
}
add_action( 'edd_sl_store_license', 'edd_auto_activate_activate_license', 10, 3 ); // Hook when license is created
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment