Looking to do enable or disable shortcode in a function, depending on a value. For instance, if someone says purchases a license key to use some shortcode on their site, I’d like to have the shortcode become disabled if they license expires. Example function I’ve been working on below:
my_premium_shortcodes(); // Fires early in my php file
function my_premium_shortcodes(){
$status = get_option( 'key_status' );
if( $status !== false && $status == 'valid' ) {
add_shortcode('shortcode_handle_1','shortcode_function_1');
add_shortcode('shortcode_handle_2','shortcode_function_2');
} else {
remove_shortcode('shortcode_handle_1','shortcode_function_1');
remove_shortcode('shortcode_handle_2','shortcode_function_2');
}
}
Any input on my approach or if the function should be handled in a separate way would be appreciated. As it stands, the shortcodes are working when everything is enabled, but stay enabled even after the ‘status’ changes to ‘invalid’.
Tags: ph