Last active
September 10, 2020 11:10
-
-
Save shamim2883/e418a49f32099883e3b47dcb0388fe99 to your computer and use it in GitHub Desktop.
Redirect EDD license renew request to Freemius
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('add_meta_boxes', function(){ | |
add_meta_box('sp_edd_fs_metabox', 'Freemius data', 'sp_edd_fs_metabox', 'download', 'side', 'default' ); | |
}); | |
function sp_edd_fs_metabox( $post ){ | |
$plugin_id = get_post_meta( $post->ID, '_sp_edd_fs_plugin_id', true ); | |
$coupon = get_post_meta( $post->ID, '_sp_edd_fs_coupon', true ); | |
echo '<p><label for="_sp_edd_fs_plugin_id">Plugin ID</label><input id="_sp_edd_fs_plugin_id" type="number" value="' . esc_attr( $plugin_id ) . '" name="_sp_edd_fs_plugin_id"></p>'; | |
echo '<p><label for="_sp_edd_fs_coupon">Coupon</label><input id="_sp_edd_fs_coupon" type="text" value="' . esc_attr( $coupon ) . '" name="_sp_edd_fs_coupon"></p>'; | |
echo '<p class="description">Set a coupon if you want to give users a discount</p>'; | |
} | |
add_filter( 'edd_metabox_fields_save', function($fields){ | |
$fields[] = '_sp_edd_fs_plugin_id'; | |
$fields[] = '_sp_edd_fs_coupon'; | |
return $fields; | |
}); | |
add_action( 'template_redirect', function(){ | |
if( ! function_exists( 'edd_is_checkout' ) || ! function_exists( 'edd_software_licensing' ) || ! edd_is_checkout() ){ | |
return; | |
} | |
$license_key = isset( $_GET['edd_license_key'] ) ? $_GET['edd_license_key'] : ''; | |
$download_id = edd_software_licensing()->get_download_id_by_license( $license_key ); | |
if( ! $download_id ){ | |
return; | |
} | |
$plugin_id = get_post_meta( $download_id, '_sp_edd_fs_plugin_id', true ); | |
$coupon = get_post_meta( $download_id, '_sp_edd_fs_coupon', true ); | |
if( !$plugin_id ){ | |
return; | |
} | |
$args = [ | |
'license_key' => substr( $license_key, 0, 32 ), | |
]; | |
if( $coupon ){ | |
$args['coupon'] = $coupon; | |
} | |
$url = add_query_arg( $args, "https://checkout.freemius.com/mode/dialog/plugin/{$plugin_id}" ); | |
wp_redirect( $url ); | |
exit; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After added this code to child theme's functions.php (or in any custom plugin) you will see 2 new options in EDD download edit page under heading "Freemius data". Get those values from freemius and add there.
Also check license key formation if you need to change. As freemius support 32 characters license key, so i used substr( $license_key, 0, 32 ). You can change it according to your requirement.