Disqus plugin for Tentacle CMS

Since the release of my personal site using Tentacle CMS I had also needed to build a few plugins to cover the basics. I am going to cover off how I built the Disqus in about 20 minutes for my CMS.

Before we touch any code you must register an account and site with Disqus.

The plugin

Every plugin must have a file the matches the folders name.

{plugin_name}/{plugin_name}.php

The plugin file must contain valid YAML inside a DocBlock at the top of your PHP file, the following meta data should be used.

This is the brains of the whole operation, Everything your plugin does will be controlled here.

A bare bones plugin will be comprised of two elements, The registration of an event, and the actual event that is being triggered. In this example however Disqus will be loaded through a static method call in the theme.

/**
name: Disqus Comments
url: http://adampatterson.ca
version: 1.0
description: Social media sharing
author:
  name: Adam Patterson
  url: http://adampatterson.ca
*/

We will need to save a site name in order for Disqus to display the comments correctly, for this we will need to add a view to our admin page.

To do this we need to register the plugin_navigation event ( more on events here ).

event::on('plugin_navigation', 'disqus::settings_nav', 8);

We also need to tell the admin page where to look. For more on the specifics of registering menu items see the Wiki.

For this plugin we will be working within the class Disqus

class disqus { 
    // Code
}

Setting up the admin navigation we previously we registered disqus::settings_nav, lets create the necessary static method now.

static function settings_nav() {
    $nav[] = array(
        'title'     => 'Disqus Settings',
        'route'      => 'disqus_settings',
        'uri'       => 'disqus/view'
    );

    return $nav;
}

If everything went according to plan, you should see a plugin called ‘Disqus Comments’ under Plugins/Manage Plugins. Click Enable.

At the moment we have a plugin registered with our CMS, and we have setup the menu items to collect our Disqus account name, but if we click on Plugins/Disqus Settings we will you will get an error. 

Lets make a view.

In our plugin folder create a folder called view as we declared in out navigations URI. I am going to skip this code chunk but you can view the source here.

It should be noted that all options collected by plugins are posted to ‘admin/settings_plugins/disqus_settings

We are able to pull back the option by using:

get::option('disqus_account');

Now that we have registering the plugin, activating it, and collecting our account name its time to work on displaying the contact form in our blog post. 

The following is simply the Universal code straight off the site. This method accepts a page/post $id and the URL of the page/post.

Making it all work

Now that we have a method to call that will generate our comment form its time to make the necessary changes to the template so that we can display our comments.

Add the following directly after the_content() if you are using some kind of social sharing plugin you might want to include the call after render_content()

disqus::comments_form( $post->id );

Done!

Signup for my mailing list

Receive other rambings like this on design, code, and some times food.