0

I'm newbie in WP and I'm working on license validate function. I need to show a form to user to enter license key and then active the plugin. I have code like this:

Show a notice to ask license key

function show_license_notice() { ?>    
    <div class="notice notice-warning">
        <p>Please enter license key to continue</p>
        <form action="<?php echo admin_url("admin.php?page=theme_options");?>" method="post" enctype="multipart/form-data">
            <input name="license_key" type="text" value="" />
            <input name="validate" type="submit" value="Validate" />
        </form>
    </div>
<?php }
add_action( 'admin_notices' , 'show_license_notice'); // run it

next I have a validate function

function validate_license($licenseKey){ ... }

when enter key, submit form, I have code:

// when submit license key
if(isset($_POST['validate']))
{
    $licenseKey = $_POST["license_key"]; // get key from the validate from
    $response = validate_license($licenseKey); // validate it

    if($response->status == 'VALID') {
        add_action('admin_notices', 'license_valid'); // show success notice
        remove_action('admin_notices', 'show_license_notice', 1); // remove the validate form
    } if($response->status == 'INVALID') {
        add_action('admin_notices', 'license_invalid');
    } if($response->status == 'EXPIRED') {
        add_action('admin_notices', 'license_expired');
    } if($response->status == '') {
        add_action('admin_notices', 'license_null');
    }
}

I want after active plugin, it will show the validate form notice, disabled all features of the plugin. After user submit license key, if successful, it will remove this validate form forever. But when I reload page, it show the form again, I'm unable to remove it.

How can I fix this? How to make this validate form run when plugin active, remove it after validate successed?

Thank you so much.

1 Answer 1

1

You could try adding a function to for example init action, that either enables plugin features (i.e. includes plugin files) or displays the admin notice.

Something along these lines,

function licence_notice_or_enable_features() {  
  // Check if (valid) licence key (or validation token, etc.) is saved to the database
  // and add other plugin files
  // If the option does not exist or does not have a value, 
  // then the return value will be false and notice is shown
  if ( get_option( 'license_key' ) ) {
    require_plugin_parts();
  } else {
    add_action( 'admin_notices', 'show_license_notice' );
  }
}
add_action( 'init', 'licence_notice_or_enable_features' );

function require_plugin_parts() {
  $plugin_path = plugin_dir_path( __FILE__ );
  // require $plugin_path . 'file.php';
}

function show_license_notice() {
  // notice and licence form html
}

You could put this to your main plugin file.

1
  • I did it, it works! Thank you for help. It works as I want.
    – Kien Pham
    Commented May 28, 2019 at 3:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.