How do I create a new plugin?
avatar
Señor FAQ

¡Hola, amigos! I’m Señor FAQ, the mustached maestro of questions and answers! With my trusty glasses and a book of endless wisdom, I turn dudas into solutions. Soy el héroe de los curiosos and the champion of clarity.


How to Create a New Plugin in JScms

Creating a plugin for JScms involves defining the plugin's metadata, registering hooks, and adding functionality. Here's a step-by-step guide using the demo plugin hello_world as an example:

1. Create the Plugin Directory

Create a new folder in the /plugins directory. For this example, we'll name the folder hello_world.

2. Add a manifest.json File

Inside the hello_world folder, create a manifest.json file. This file contains the plugin's metadata:

{
    "name": "Hello World",
    "description": "A simple plugin that displays a message in the footer.",
    "version": "1.0",
    "author": "JScms",
    "installfile": "",
    "uninstallfile": "",
    "updatefile": "",
    "pluginpath": "hello_world/hello_world.php",
    "plugindirectory": "hello_world",
    "license_required": "0",
    "license_api": "https://jscms.ch"
}

Fields explained:

  • name: The plugin's display name.
  • description: A short description of the plugin.
  • version: The version number of the plugin.
  • author: The plugin creator's name.
  • installfile: (Optional) Path to a script that runs during installation.
  • uninstallfile: (Optional) Path to a script that runs during uninstallation.
  • updatefile: (Optional) Path to a script for updates.
  • pluginpath: The relative path to the main plugin PHP file.
  • plugindirectory: The directory name where the plugin is located.
  • license_required: Set to "1" if the plugin needs a license to install.
  • license_api: The URL to the license API.

3. Create the Main Plugin File

Create a file named hello_world.php in the plugin folder. This file contains the plugin's functionality:

<?php

use JScmsClassDynamicHooks;

// Prevent direct access
if (!defined('JSCMS_PREVENT_PLUGIN_ACCESS')) {
    die('Direct access not allowed.');
}

// Example hook: Add a message to the footer
DynamicHooks::addHook('after_render', function ($jkv, $lang) {
    echo "Hello, World! This is a message from the footer plugin.";
}, 'hello_world', 15);

// Example hook: Add a message to the admin footer
DynamicHooks::addHook('after_render_admin', function ($jkv, $lang) {
    echo "Hello, Admin! Footer plugin is active.";
}, 'hello_world', 15);
?>

Code explained:

  • The DynamicHooks::addHook() function registers hooks with a name, callback function, and priority.
  • Hook Name: after_render is called after rendering the page, while after_render_admin applies to the admin panel.
  • Callback: The anonymous function contains the logic to execute when the hook is triggered.
  • Priority: A number determining the execution order of hooks. Lower numbers execute first.

4. Test the Plugin

  1. Upload the hello_world folder to the /plugins directory.
  2. Log in to the JScms admin panel and navigate to the "Plugins" section.
  3. Activate the Hello World plugin.
  4. Visit your site or admin panel to see the footer message added by the plugin.

5. Best Practices

  • Always use the JSCMS_PREVENT_PLUGIN_ACCESS constant to prevent direct access to plugin files.
  • Test your plugin in a development environment before deploying to production.
  • Follow proper naming conventions for files, classes, and hooks to avoid conflicts.
Was this article helpful?
0 out of 0 found this helpful