WordPress Plugin Integration

Add license validation to your WordPress plugin in just a few lines of code.

For AI Coding Tools

This guide is designed to be copied into AI coding assistants. It contains everything needed to implement KeyKit license validation in any WordPress plugin that uses Stripe for payments.

How to Use

1

Copy the Instructions

Click "Copy All" below to copy the complete WordPress integration guide

2

Paste into Your AI Tool

Paste the instructions into Cursor, Claude, Lovable, or any AI coding assistant

3

Ask for Implementation

Ask your AI to implement KeyKit license validation in your WordPress plugin

4

Get Your Credentials

No additional credentials needed - just use your license key!

Prerequisites
What you need before implementing
  • • Stripe account with products
  • • KeyKit account (sign up at keykit.app)
  • • WordPress plugin with admin capabilities
  • • Stripe products marked with "keykit_eligible=true"
  • • Webhook configured in Stripe Dashboard
What You Get
Features included with KeyKit
  • • Automatic license key generation
  • • Real-time license validation
  • • Stripe subscription sync
  • • WordPress admin integration
  • • Customer management dashboard
WordPress Integration Instructions
Copy this entire block into your AI coding tool
# KeyKit WordPress Plugin License Validation Integration

## Context
KeyKit is a Stripe-connected license key management service. When customers buy your WordPress plugin on Stripe, KeyKit automatically creates license keys and emails them to customers. 

**Your WordPress plugin only needs to check if a key is valid - KeyKit handles all the complex license management, renewal, and status tracking.**

## Setup Requirements
- Customer must have a Stripe account
- Products must be marked with "keykit_eligible=true" in Stripe product description
- KeyKit webhook must be configured in Stripe Dashboard
- WordPress plugin with admin settings capability

## Integration Steps

### 1. License Validation Function
Create a simple function to check if a license key is valid:

**NO pre-validation needed - just send the key to KeyKit and trust the response**

```php
function check_license($license_key, $site_url) {
    $response = wp_remote_post('https://keykit.app/api/validate', [
        'headers' => [
            'Content-Type' => 'application/json'
        ],
        'body' => json_encode([
            'token' => $license_key,
            'siteId' => $site_url
        ])
    ]);
    
    if (is_wp_error($response)) {
        return false;
    }
    
    $body = wp_remote_retrieve_body($response);
    $data = json_decode($body, true);
    
    return $data['valid'] ?? false;
}
```

> **Keep it simple**: Just return true/false. KeyKit handles all the complex license management!
> **NO pre-validation needed**: Don't check format, length, or anything else - just send to KeyKit

### 2. License Check on Plugin Load
Add license validation when your plugin initializes:

```php
// Check license when plugin loads
add_action('init', function() {
    $license_key = get_option('my_plugin_license_key');
    $site_url = home_url();
    
    if (!check_license($license_key, $site_url)) {
        // Show license error or disable features
        add_action('admin_notices', function() {
            echo '<div class="notice notice-error"><p>Invalid license key. Please check your license.</p></div>';
        });
        
        // Optionally disable premium features
        add_filter('my_plugin_premium_features', '__return_false');
    }
});
```

### 3. License Settings Page
Add a settings page for customers to enter their license key:

```php
// Add license field to settings page
add_action('admin_menu', function() {
    add_options_page(
        'Plugin License',
        'Plugin License',
        'manage_options',
        'plugin-license',
        function() {
            if (isset($_POST['license_key'])) {
                $license_key = sanitize_text_field($_POST['license_key']);
                $site_url = home_url();
                
                if (check_license($license_key, $site_url)) {
                    update_option('my_plugin_license_key', $license_key);
                    echo '<div class="notice notice-success"><p>License key saved and validated!</p></div>';
                } else {
                    echo '<div class="notice notice-error"><p>Invalid license key. Please check and try again.</p></div>';
                }
            }
            
            $current_key = get_option('my_plugin_license_key', '');
            echo '<form method="post">';
            echo '<table class="form-table">';
            echo '<tr><th>License Key</th><td><input type="text" name="license_key" value="' . esc_attr($current_key) . '" class="regular-text" placeholder="Enter your license key" /></td></tr>';
            echo '</table>';
            echo '<input type="submit" class="button-primary" value="Save License" />';
            echo '</form>';
        }
    );
});
```

### 4. Premium Feature Protection
Protect premium features with license validation:

```php
// Example: Protect a premium feature
function my_premium_feature() {
    $license_key = get_option('my_plugin_license_key');
    $site_url = home_url();
    
    if (!check_license($license_key, $site_url)) {
        wp_die('This feature requires a valid license key. Please enter your license key in the settings.');
    }
    
    // Your premium feature code here
    echo "Premium feature content";
}

// Only show premium features if license is valid
add_action('wp_ajax_my_premium_action', function() {
    my_premium_feature();
});
```

### 5. License Status Display
Show license status in admin dashboard:

```php
// Add license status to admin dashboard
add_action('wp_dashboard_setup', function() {
    wp_add_dashboard_widget(
        'my_plugin_license_status',
        'Plugin License Status',
        function() {
            $license_key = get_option('my_plugin_license_key');
            $site_url = home_url();
            
            if (empty($license_key)) {
                echo '<p>No license key entered. <a href="' . admin_url('options-general.php?page=plugin-license') . '">Enter license key</a></p>';
            } elseif (check_license($license_key, $site_url)) {
                echo '<p style="color: green;">✓ License is valid and active</p>';
            } else {
                echo '<p style="color: red;">✗ License is invalid. <a href="' . admin_url('options-general.php?page=plugin-license') . '">Update license key</a></p>';
            }
        }
    );
});
```

## Customer Experience
- Customers receive license keys via email immediately after purchase
- License keys are formatted as: `lk_1234567890abcdef1234567890abcdef`
- Keys are safe to share with customers and work immediately
- Customers enter the key in your WordPress plugin settings to activate their license

## Important Notes
- **Keep it simple**: Just check if the key is valid (true/false)
- **KeyKit handles everything else**: License creation, renewal, status tracking, etc.
- **NO pre-validation needed**: Don't check format, length, or anything else - just send to KeyKit
- **Trust KeyKit's response**: If KeyKit says it's valid, it's valid
- Validate on the server (WordPress/PHP) when possible
- Handle network errors gracefully to avoid app crashes
- License keys are automatically created and emailed when customers buy on Stripe
- License status syncs automatically with Stripe subscription status
- Use WordPress sanitization functions for all user inputs

## Error Handling
- Network errors: Handle gracefully, don't crash the app
- Invalid keys: Show clear error message and upgrade prompt
- Consider caching to reduce API calls
- WordPress errors: Use wp_die() for fatal errors, admin_notices for warnings

## Security
- Always validate on server-side for production plugins
- Use HTTPS for all API calls
- Sanitize all user inputs with WordPress functions
- Use nonces for form submissions
- Implement proper capability checks for admin functions

## WordPress Best Practices
- Use WordPress hooks and filters appropriately
- Follow WordPress coding standards
- Use WordPress database functions (get_option, update_option)
- Implement proper activation/deactivation hooks
- Use WordPress internationalization functions
- Follow WordPress security guidelines

How It Works

Your customers buy your plugin on your website (using Stripe), and KeyKit automatically creates a license key. Your plugin checks this key to verify the customer has a valid license.

Basic Integration

1. Add License Check Function

function check_license($license_key, $site_url) {
    $response = wp_remote_post('https://keykit.app/api/validate', [
        'headers' => [
            'Content-Type' => 'application/json',
            'x-org-id' => 'your-org-id',
            'x-keykit-signature' => 'your-signature'
        ],
        'body' => json_encode([
            'token' => $license_key,
            'siteId' => $site_url
        ])
    ]);
    
    if (is_wp_error($response)) {
        return false;
    }
    
    $body = wp_remote_retrieve_body($response);
    $data = json_decode($body, true);
    
    return $data['valid'] ?? false;
}

2. Use in Your Plugin

// Check license when plugin loads
add_action('init', function() {
    $license_key = get_option('my_plugin_license_key');
    $site_url = home_url();
    
    if (!check_license($license_key, $site_url)) {
        // Show license error or disable features
        add_action('admin_notices', function() {
            echo '<div class="notice notice-error"><p>Invalid license key. Please check your license.</p></div>';
        });
    }
});

3. Add License Input Field

// Add license field to settings page
add_action('admin_menu', function() {
    add_options_page(
        'Plugin License',
        'Plugin License',
        'manage_options',
        'plugin-license',
        function() {
            if (isset($_POST['license_key'])) {
                update_option('my_plugin_license_key', sanitize_text_field($_POST['license_key']));
                echo '<div class="notice notice-success"><p>License key saved!</p></div>';
            }
            
            $current_key = get_option('my_plugin_license_key', '');
            echo '<form method="post">';
            echo '<table class="form-table">';
            echo '<tr><th>License Key</th><td><input type="text" name="license_key" value="' . esc_attr($current_key) . '" class="regular-text" /></td></tr>';
            echo '</table>';
            echo '<input type="submit" class="button-primary" value="Save License" />';
            echo '</form>';
        }
    );
});

That's It!

Your WordPress plugin now validates licenses automatically. When customers buy your plugin, they'll get a license key that your plugin can verify.

Need Help?

If you need assistance with WordPress integration, check our documentation or contact support.