1. Home
  2. Knowledge Base
  3. Developer
  4. Example Theme Integration

Example Theme Integration

Installation

To integrate the client library into your WordPress Theme, you need to first include the client library in your project. You can do so by downloading the library from the GitHub repository or by installing it using the composer dependency manager.

Run the following command to install using composer

composer require digitalduz/slswc-client

Theme Integration

$license_details = array(
    'license_key' => 'THE_LICENSE_KEY', // Required.
    'domain' => 'THE_CURRENT_DOMAIN', // Optional: will default to WordPress site url.
    'slug' => 'plugin-slug', // optional. Will use plugin text domain. Must be the product slug on license server.
);

$theme_dir_name = WP_CONTENT_DIR . '/themes/theme-directory-name';
$theme =  Theme::get_instance( 'http://example.test/', $theme_dir_name, $license_details );
$theme->init_hooks();

$license_server_url – This is the domain of the license server.
$theme_dir_name – The path to the folder containing the Theme’s main styles.css which defines the The’s headers.
$license_details – This is an array of key-value pairs required for the license server to activate the license. At the bare minimum, you need the license_key and the other details will be extracted from the plugin’s headers.

Remember to change ‘theme-folder-name’ with the actual name of the folder containing your theme’s style.css and functions.php files.

The $license_details array expects the following array keys:

  • license_key – The license key from the license server. This can be stored and retrieved from anywhere, like in wp_options table or .env file
  • domain – The current domain where the plugin is being installed or activated. The client library will assume the WordPress site URL is the domain name if no domain is given.
  • slug – The plugin’s unique name. This must also be the product slug on the license server.
  • version – The current version of the plugin which is being activated. This will be used to check for updates. If this is not provided, the Version from the plugin header will be used.

Activating the license

This is an example of how to activate the license. You can hook this to a form save after saving the license key.

// Example of how to update the plugin. Run this on a hook.
if ( $theme->license->get_license_status() !== 'active' ) {
	$theme->license->validate_license();
}

Full example

Add the following code to functions.php in the theme:

require __DIR__ . '/vendor/autoload.php';

use Digitalduz\Slswc\Client\Theme;

function theme_slswc_client() {
    $license_details = array(
        'license_key' => 'LICENSE_KEY',        // Required
        'domain'      => 'THE_CURRENT_DOMAIN', // Optional: will default to WordPress site url.
        'slug'        => 'plugin-slug',        // optional. Will use plugin text domain. Must be the product slug on license server.
    );
    $theme = Theme::get_instance(
        'http://example.com',
        WP_CONTENT_DIR . '/themes/theme-folder-name',
        $license_details
    );

    // Example of how to update the plugin. Run this on a hook.
    if ( $theme->license->get_license_status() !== 'active' ) {
	$theme->license->validate_license();
    }
}
add_action( 'wp_loaded', 'theme_slswc_client', 11 );

And then add the following to style.css

/*
Theme Name  : Theme Name
Theme URI   : https://example.test/themes/your-theme-name/
Author      : Author Name
Author URI  : https://example.test/
Description : Software License Server for WooCommerce Test Theme
Version     : 1.0
License     : GNU General Public License v2 or later
License URI : http://www.gnu.org/licenses/gpl-2.0.html
Tags        : blog, two-columns, left-sidebar
Text Domain : test-theme
SLSWC       : theme
Documentation URL: https://example.test/docs/test-theme
Tested WP   : 5.8
Requires WP : 5.8.1
*/
Was this article helpful?

Related Articles

Need Support?

Can't find the answer you're looking for?
Contact Support
Scroll to Top