API/Hello World

To start off, here is a simple plugin that adds a special page that simply outputs the obligatory "Hello World" message on the page. This is the entire plugin - you can copy and paste this into a new .php file, drop it into your plugins folder, and it should show up right away in the Plugin Manager section of the admin CP.

Note: All of the example code in this API documentation is for Enano 1.1.x. Don't bother using any of it on Enano 1.0.x - it won't work.

<?php
/**!info**
{
  "Plugin Name"  : "Hello World",
  "Plugin URI"   : "http://example.com/",
  "Description"  : "Hello world plugin",
  "Author"       : "Dan Fuhry",
  "Version"      : "1.0",
  "Author URI"   : "http://enanocms.org/"
}
**!*/
 
// Add our special page
$plugins->attachHook('session_started', 'register_special_page(\'HelloWorld\', \'Hello World!\', true);');
 
// Special page
function page_Special_HelloWorld()
{
  global $output;
  $output->header();
  echo '<p>Hello world!</p>';
  $output->footer();
}

So, what's in this?

As you can see, this plugin isn't too complicated. Here are the elements we use in here.

Metadata

That comment block at the top is an !info metadata block. All Enano plugins need one, because the plugin manager uses that to tell you information about the plugin in the administration panel. Plugins without a valid metadata block are ignored.

Metadata blocks are one of the many places Enano uses JSON. There's nothing difficult about JSON, and it's a highly standardized format (although Enano's parser permits some leniency in certain areas for the sake of code elegance).

Attaching to a hook

The first actual code in this plugin is where we attach to a hook. Inclusion of plugin files is done at a pretty early stage, when a lot of Enano core classes aren't set up yet. So we use hooks to let you schedule code to be executed at a later point.

This example has code right inside the hook attachment, but a lot of people choose to call a function inside the hook attachment and put the real code in there. This lets you cut down on the amount of escaping you have to do and lets you write code as neat as possible.

Creating a special page

Inside the hook we see a call to a function called register_special_page. This function is what you use to tell Enano you have a new special page that you want to make available. In this example, if our Enano website is installed at http://example.com/ and we're using Rewritten URLs, the URL to this special page would be http://example.com/Special:HelloWorld.

Registering a special page also causes it to immediately show up in places like search results.

Special pages use a simple naming convention for functions that is explained in the special page API documentation. You can see that we also have created a function named page_Special_HelloWorld. This function is called when the user requests our new special page.

Finally, inside the function for our special page we can see two method calls: $output->header() and $output->footer(). These output the start of the page - everything up until where you want to start writing content - and the last of the page, respectively.

Finally we echo out our message. The page title is set in the call to register_special_page, but you can also set it manually inside your special page function by calling $output->set_title() at any point before $output->header().

Excited?

Yep, writing plugins for Enano is that easy. Ready to dive in? This API documentation includes examples for just about every major feature of the Enano API, so have some fun and experiment with Enano.

Categories: (Uncategorized)