Tutoriel : Blog


Mis à jour pour Elgg 1.8

Ce tutoriel va vous montrer comment construire un plugin simple de blog avec Elgg. Vous aurez besoin d'enlever le plugin blog fourni avec Elgg pour utiliser ce tutoriel.

  1. Le module blog
  2. Créer un nouveau billet de blog
  3. Le formulaire pour créer le nouveau billet de blog
  4. Le fichier action
  5. L'objet Vue
  6. le start.php du plugin
  7. Déclarer un gestionnaire de billet
  8. Tester le module
  9. Afficher la liste des billet
  10. La page de blog d'un utilisateur 
  11. Notes Pre-1.8


Le module blog

Elgg a un répertoire /mod. Créer un sous-répertoire "blog". Le nom du répertoire devient le nom de votre plugin. Dans la foulée, créez quelques autres sous répertoires : 




Vous aurez également besoin d'ajouter un fichier manifest.xml dans /mod/blog/ qui contient les informations de base à propos du plugin. Voir ce tutoriel pour le modèle. Vous pouvez également juste copier un fichier manifest d'un autre plugin et changer les valeurs pour qu'ils correspondent à votre nouveau plugin.

Créer un nouveau billet de blog

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.



The form for creating a new blog post

Create a file called save.php in /mod/blog/views/default/forms/blog/ that contains a form. This is the view that is called above: elgg_view_form("blog/save"). The form should have input fields for the title, body and tags. Because we used elgg_view_form, we do not need to output the form tags itself. The view will be automatically wrapped with a form and secure tokens, and will automatically direct to "action/blog/save":



     'title')); ?>





     'body')); ?>





     'tags')); ?>




     elgg_echo('save'))); ?>


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.


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:


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

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


The action will now be available as /action/blog/save. By default, all actions are available only to logged in users. If you want to make an action available to only admins or open it up to unauthenticated users, you can pass 'admin' or 'public' as the third parameter of elgg_register_action().

Registering a page handler

In order to be able to serve the page that generates the form, you'll need to register a page handler. Add the following to your start.php:

elgg_register_page_handler('blog', 'blog_page_handler');


function blog_page_handler($segments) {

    if ($segments[0] == 'add') {

        include dirname(__FILE__) . '/pages/blog/add.php';



Trying it out

If you have not enabled the plugin yet, you will need to go to Administration => Configure => Plugins => Advanced. 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/blog/add. Try it out.

Displaying list of blogs

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

Create /mod/blog/pages/blog/all.php.

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

$body = elgg_list_entities(array(

    'type' => 'object',

    'subtype' => 'blog',


The function `elgg_list_entities` (and its cousins) also transparently handle 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);


echo elgg_view_page("Our blog page", $body);

A user's 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 specifying the owner_guid argument in the list function above.

echo elgg_list_entities(array(

    'type' => 'object',

    'subtype' => 'blog',

    'owner_guid' => elgg_get_logged_in_user_guid(),



Pre-1.8 notes

There are several new functions introduced in 1.8 that deprecated the old, inconsistently named functions of 1.7 and below. They are listed here:


was page_draw (and did not require `echo`)


was register_page_handler


was register_action, and had a different argument order.