Menu

Creating your first plugin

albi90

Creating your first plugin

All plugins should be placed in the root directory of node-screen in the plugin folder.

The basic file structure is as follows:
/plugins/PLUGIN_NAME/PLUGIN_NAME.php
PLUGIN_NAME should be replaced with the name of you plugin, so each plugin has a folder and a php script with the same name.

Let’s create a plugin called myplugin, first create a directory called myplugin in your /plugins dir, then create a file called myplugin.php inside this directory.

Open myplugin.php file, We start the plugin by adding in some comments, these comments are structured to give our plugin management system the details about the plugin, add the following to your myplugin.php file, and save the file

<?php
/**

* Plugin Name: My first plugin
* Plugin URL: http://myurl.com/plugin
* Version: 1.0
* Description: this is my first plugin for node-screen
* Author: my name
* Author URL: http://myurl.com
*/
?>

Open the node-screen server as an admin and click on your name then “Plugin Management” you should see in the list of plugins “My first plugin”.


To make the plugin work we need add actions, by default each plugin has 4 main actions:

Action Description
PLUGIN_NAME.install This action is called when the plugin is installed, you should use this action to create database entries or copy files needed by your plugin
PLUGIN_NAME.uninstall This action is called when your plugin is uninstalled, you should remove unwanted plugin files and database entries here
PLUGIN_NAME.activate This action is called when your plugin is activated any final installations steps should be run by this action
PLUGIN_NAME.deactivate This function is called when your plugin is deactivated

We can also add custom actions as defined by our action list here, for this plugin we want to change the Text for “Screens” in the navigation bar so we will need to hook the action “*.nav_data” which gets passed the navbar data for us to manipulate, change myplugin.php to have the flowing code

<?php
/**

* Plugin Name: My first plugin
* Plugin URL: http://myurl.com/plugin
* Version: 1.0
* Description: this is my first plugin for node-screen
* Author: my name
* Author URL: http://myurl.com
*/

add_action('*.nav_data', 'change_navbar');

function change_navbar($nav_data){
    foreach($nav_data['navbar_buttons'] as $key=>$value){
        if($value['text'] == 'Screens'){
            $nav_data['navbar_buttons'][$key]['text'] = 'Changed By Plugin' ;
        }
    }
    return $nav_data;
}


Lets break this down into 3 sections

We defined the plugin details

/**

* Plugin Name: My first plugin
* Plugin URL: http://myurl.com/plugin
* Version: 1.0
* Description: this is my first plugin for node-screen
* Author: my name
* Author URL: http://myurl.com
*/



We added an action that will be called by all controllers, this action receives and returns the navbar data

add_action('*.nav_data', 'change_navbar');



We then defined the function to manipulate and return the navbar data

function change_navbar($nav_data){
    foreach($nav_data['navbar_buttons'] as $key=>$value){
        if($value['text'] == 'Screens'){
            $nav_data['navbar_buttons'][$key]['text'] = 'Changed By Plugin' ;
        }
    }
    return $nav_data;
}



Save the file and install/activate the plugin you should now see “Changed By Plugin” in the navigation bar instead of “Screens”


Related

Wiki: Home

MongoDB Logo MongoDB