Custom plugins are mini web applications that integrate seamlessly with the Staffbase platform to provide tailored functionality. Administrators can add and publish them in the same way they manage standard Staffbase plugins. Learn more about plugins.
In this tutorial, you will:
- Develop a minimal custom plugin using PHP.
- Access and apply your organization’s theme text and background colors to content and buttons in your custom plugin.
- Align your custom plugin UI with your Staffbase platform.
By the end, you’ll be familiar with the basics of aligning your custom plugin with your branding, an essential step for creating a visually cohesive user experience. This can be done to any custom plugin and allows you to align your plugin visually with the rest of your Staffbase platform.
This tutorial is designed to help you gain hands-on experience with plugin development for the Staffbase platform. It walks you through developing the plugin locally. To use it on the Staffbase platform, you will need to complete the following, which are not covered in the tutorial:
- Share the plugin specifications with Staffbase.
- Host the plugin on a server that can run PHP files and is reachable over the internet.
- Register and activate the plugin on the Staffbase platform. Contact Staffbase Support or your Customer Success Manager for more information.
- Have your Staffbase administrator install and publish it. Learn more about making content available via a plugin.
Prerequisites
- You have a basic understanding of Staffbase plugins and are familiar with terminal/command line usage.
- PHP is installed on your system.
- You have a code editor. For example, Visual Studio Code.
Download composer
To start building the plugin, you need to use the plugin SDK for PHP. This SDK provides the basic functionality to parse and verify the PHP token. Staffbase provides the SDK via a composer.
Run the following commands in your terminal:
- Create a folder for your project.
mkdir MyCustomPlugincd MyCustomPlugin
- Download the composer installer to the current directory.
You should now see a file namedcomposer.phar
in your folder.
Install dependencies
Now you can use the composer to install the specific dependencies required for the plugin SDK.
- Install the custom plugin dependencies using composer.
./composer.phar require staffbase/plugins-sdk-php
- List the files installed.
ls -l
You should see:
composer.json
composer.lock
vendor
These are your dependency and package management files.
Open the folder in the code editor
- Launch your project folder
MyCustomPlugin
, in your preferred code editor. It looks as shown in the screenshot:
Develop the plugin
Now that you have all the dependencies, you can create PHP files and a structure for plugin development.
Inside the vendor folder, you have the plugin SDK and all other dependencies it requires.
- Create an
index.php
file inside the project folder. In theindex.php
file, provide a namespace for the plugin.
<?php
namespace MyCustomPlugin;
- Import the library from the SDK to verify the SSO token. The SSO token here is a JSON Web Token that verifies whether the information is valid.
use Staffbase\plugins\sdk\SSOToken;
- Authenticate the user with an app secret.
try {
$appSecret = '{enter your secret here}';
The $appSecret is a public key that the plugin uses to verify the signature of any JWT received. For now, you can leave it blank. You receive the key from Staffbase when you register your plugin with Staffbase and need to provide it if you want to make the plugin available on the Studio Plugins page.
- Use the
SSOToken
class to validate the incoming JWT token. This typically happens via aGET
request when a user accesses your plugin:
$sso = new SSOToken($appSecret, $_GET['jwt'] ?? '');
Here’s what a sample request looks like:
https://my.plugin.com/index.php?jwt=MII3ksglkgfglfgnfdl
This step ensures that the user is authenticated and the token data is valid before rendering any content or executing plugin logic.
- Use the exception library to add an exception rule for when the user accessing the plugin does not have the required permissions.
use Exception;
} catch (\Exception $e) { print "Access denied."; exit;}
Apply theme colors to plugin content
You can now ensure your custom plugin aligns with your Staffbase platform’s active theme. Use the following code snippet to apply the theme’s text and background colors to content and buttons:
print "<html> <body> <h1 style='color: " . $sso->getThemeTextColor() . ";'>Hello!</h1> <button style='background-color: " . $sso->getThemeBackgroundColor() . "; color: " . $sso->getThemeTextColor() . ";'> Open Link </button> </body></html>";
The getThemeTextColor() method retrieves the appropriate text color from the decoded JWT for the user. This color is used for both the h1 element and the button text. The getThemeBackgroundColor() method retrieves the background color defined by the active theme. This color is used for the button background.
The entire code snippet in the index.php
file should look like this:
<?php
namespace MyCustomPlugin;
use Staffbase\plugins\sdk\SSOToken;
try {
$appSecret = '{enter your secret here}'; $sso = new SSOToken($appSecret, $_GET[`jwt`]?? "");
// https://my.plugin.com/index.php?jwt={enter-the-jwt-token}
// Display a personalized greeting
print "<html> <body> <h1 style='color: " . $sso->getThemeTextColor() . ";'>Hello!</h1> <button style='background-color: " . $sso->getThemeBackgroundColor() . "; color: " . $sso->getThemeTextColor() . ";'> Open Link </button> </body> </html>";
} catch (\Exception $e) { print "Access denied. "; exit;}
You’ve successfully developed a custom plugin that visually aligns with the rest of your Staffbase platform. The example above is the foundation for visually aligning to your brand colors.
Recommended next steps for the Brand Alignment plugin
- Host your plugin on a server
- Share specifications with Staffbase
- Register and activate your plugin
- Ask your Staffbase administrator to install and publish it