Tutorials/Blog

This tutorial will show you how to build a simple blog plugin using Elgg. You will need to remove the blog plugin that comes with Elgg to use this tutorial.


Contents

The blog module

Elgg has a /mod directory. Create a subdirectory there called "blog". The name of this directory becomes the name of your plugin. In turn, create a few more subdirectories so we have:

/mod/blog/
/mod/blog/actions/
/mod/blog/views/default/

It's a good idea to add a manifest file in /mod/blog/, which stores basic information about the plugin. See this tutorial for the template. You can also just copy the manifest file from another plugin and then change the values to fit your new plugin.

Create a new blog post page

First, we need to create a page that enables a user to create a blog post.

Create the file add.php in /mod/blog/ and add this code.

The comments should explain what is going on.


<?php
    // Load Elgg engine
    include_once(dirname(dirname(dirname(__FILE__))) . "/engine/start.php");
 
    // make sure only logged in users can see this page	
    gatekeeper();
 
    // set the title
    $title = "Create a new blog post";
 
    // start building the main column of the page
    $area2 = elgg_view_title($title);
 
    // Add the form to this section
    $area2 .= elgg_view("blog/form");
 
    // layout the page
    $body = elgg_view_layout('two_column_left_sidebar', '', $area2);
 
    // draw the page
    page_draw($title, $body);
?>

The form for creating a new blog post

Create a file called form.php in /mod/blog/views/default/blog/ that contains a form. This is the view that is called above: elgg_view("blog/form"). To read more about the Elgg view system, go here. The form should have input fields for the title, body and tags, and direct to "<?php echo $vars['url']; ?>action/blog/save":

<div class="contentWrapper">
<form action="<?php echo $vars['url']; ?>action/blog/save" method="post">
 
<p><?php echo elgg_echo("title"); ?><br />
<?php echo elgg_view('input/text',array('internalname' => 'title')); ?></p>
 
<p><?php echo elgg_echo("body"); ?><br />
<?php echo elgg_view('input/longtext',array('internalname' => 'body')); ?></p>
 
<p><?php echo elgg_echo("tags"); ?><br />
<?php echo elgg_view('input/tags',array('internalname' => 'tags')); ?></p>
 
<?php echo elgg_view('input/securitytoken'); ?>
 
<p><?php echo elgg_view('input/submit', array('value' => elgg_echo('save'))); ?></p>
 
</form>
</div>

Notice how the form is calling input views like input/longtext. These are built into elgg and make it easy to add form components. You can see a complete list of input views in the /views/default/input/ directory.

The action file

Create a file save.php in /mod/blog/actions/. This will save the blog post in the database.

<?php
  // only logged in users can add blog posts
  gatekeeper();
 
  // get the form input
  $title = get_input('title');
  $body = get_input('body');
  $tags = string_to_tag_array(get_input('tags'));
 
  // create a new blog object
  $blogpost = new ElggObject();
  $blogpost->title = $title;
  $blogpost->description = $body;
  $blogpost->subtype = "blog";
 
  // for now make all blog posts public
  $blogpost->access_id = ACCESS_PUBLIC;
 
  // owner is logged in user
  $blogpost->owner_guid = get_loggedin_userid();
 
  // save tags as metadata
  $blogpost->tags = $tags;
 
  // save to database
  $blogpost->save();
 
  // forward user to a page that displays the post
  forward($blogpost->getURL());
?>

A few fields are built into Elgg objects. Title and description are two of these; as a result it makes sense to use description to contain the blog text. Every entity can have a subtype and in this we are using "blog". The tags are stored as metadata.


Every object in Elgg has a built-in URL automatically, although you can override this if you wish. getURL() is called to get that unique URL.

The object view

Elgg will automatically call the object/blog view to view the blog post so we need to create the object view.

Objects in Elgg are a subclass of something called an "entity". (Users and sites are also subclasses of entity.) All entities can have a subtype, the meat of which is utterly up to you. Valid object subtypes include blog posts, calendar entries, loaves of bread, chocolate teapots and inflatable dartboards. Here, we have used the subtype "blog" to identify a blog post.

In /mod/blog/views/default/, create a folder /object/ and then create a file blog.php in it.

Each blog post will be passed to this PHP file as $vars['entity']. ($vars is an array used in the views system to pass variables to a view.) Given this, the content of object/blog.php can just be something like:

<?php echo elgg_view_title($vars['entity']->title); ?>
 
<div class="contentWrapper">
 
<p><?php echo $vars['entity']->description; ?></p>
 
<?php echo elgg_view('output/tags', array('tags' => $vars['entity']->tags)); ?>
 
</div>

The last line takes the tags on the blog post and automatically displays them as a series of clickable links. Search is handled completely automatically.

Incidentally, if you're wondering about the 'default' in /views/default/, you can create alternative views. RSS, OpenDD, FOAF, mobile and others are all valid view types.

Plugin start.php

Every plugin has a start.php that initializes it. For this example, we just need to register the action file we created earlier: Also see a related tutorial

<?php
global $CONFIG;
 
register_action("blog/save", false, $CONFIG->pluginspath . "blog/actions/save.php");
?>

The action will now be available as /action/blog/save, although the false in the second parameter means that it will only be available to logged in users. The third parameter just defines where the source file is.

Trying it out

If you have not enabled the Plugin yet, you will need to go to Administration/Tools Administration page and scroll to the bottom until you see your plugin. Click the Enable button.

The page to create a new blog post is accessible at http://yoursite/mod/blog/add.php. Try it out.

Displaying list of blogs

Let's also create a page that lists blog entries that have been created.

Create /mod/blog/index.php.

First of all, we need to load the Elgg engine:

include_once(dirname(dirname(dirname(__FILE__))) . "/engine/start.php");

Then we'll grab the latest blog posts. Note that this function returns only the posts that the user can see, so access restrictions are handled transparently:

$body = list_entities('object','blog',0,10,false);

The function list_entities (and its cousins) also transparently handles pagination, and even creates an RSS and OpenDD feed for your blog if you have defined these views.

Finally, we'll draw the page:

$body = elgg_view_layout('one_column', $body);
 
page_draw("Our blog page",$body);

A user blog page or friends' blog page

If we grab the Global Unique IDentifier (GUID) of the logged in user, we can limit the blog posts to those posted by a particular user by replacing the list function above with:

$user_guid = get_loggedin_userid();
list_user_objects($user_guid,'blog',10,false);

Or with a list of the blog posts of that user's friends by replacing it with:

$user_guid = get_loggedin_userid();
list_user_friends_objects($user_guid,'blog',10,false);

Search docs