hello_world.rst 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. Hello world
  2. ###########
  3. This tutorial shows you how to build a simple plugin that adds a new page and
  4. prints the text "Hello world" on it.
  5. In this tutorial we will use the address ``http://www.mysite.com/`` as an example.
  6. While developing the plugin you should use the address of your own site
  7. instead of the example address.
  8. Required files
  9. ==============
  10. First of all you need a directory that will hold all the files required by the
  11. plugin. Go to the ``mod`` directory of your Elgg site and create there a
  12. directory with the name ``hello_world``.
  13. Go to the ``hello_world`` directory and create these two files inside it:
  14. - start.php
  15. - manifest.xml
  16. Copy this to the ``manifest.xml`` file:
  17. .. code-block:: xml
  18. <?xml version="1.0" encoding="UTF-8"?>
  19. <plugin_manifest xmlns="http://www.elgg.org/plugin_manifest/1.8">
  20. <name>Hello world</name>
  21. <id>hello_world</id>
  22. <author>Your name here</author>
  23. <version>1.0</version>
  24. <description>Adds a page that prints "Hello world".</description>
  25. <requires>
  26. <type>elgg_release</type>
  27. <version>1.9</version>
  28. </requires>
  29. </plugin_manifest>
  30. Add your name to the ``<author></author>`` element.
  31. The plugin has now the minimum requirements for your site to recognize it.
  32. Log in to your site as an administrator and access the plugins page at the
  33. administration panel. By default the plugin is at the bottom of the plugins
  34. list. Click the "Activate" button to start it.
  35. .. figure:: images/hello_world1.png
  36. :align: center
  37. :width: 80%
  38. The Hello world plugin has appeared to the bottom of the plugin list
  39. Initializing the plugin
  40. =======================
  41. The next step is to add some actual features. Open the ``start.php`` and copy
  42. this to it:
  43. .. code-block:: php
  44. <?php
  45. elgg_register_event_handler('init', 'system', 'hello_world_init');
  46. function hello_world_init() {
  47. }
  48. This piece of code tells Elgg that it should call the function
  49. ``hello_world_init()`` when the Elgg core system is initiated.
  50. Registering a page handler
  51. ==========================
  52. The next step is to register a page handler which has the purpose of handling
  53. request that users make to the URL http://www.mysite.com/hello/.
  54. Update the ``start.php`` to look like this:
  55. .. code-block:: php
  56. <?php
  57. elgg_register_event_handler('init', 'system', 'hello_world_init');
  58. function hello_world_init() {
  59. elgg_register_page_handler('hello', 'hello_world_page_handler');
  60. }
  61. function hello_world_page_handler() {
  62. $params = array(
  63. 'title' => 'Hello world!',
  64. 'content' => 'This is my first plugin.',
  65. 'filter' => '',
  66. );
  67. $body = elgg_view_layout('content', $params);
  68. echo elgg_view_page('Hello', $body);
  69. }
  70. The call to ``elgg_register_page_handler()`` tells Elgg that it should
  71. call the function ``hello_world_page_handler()`` when user goes to your site
  72. and has "hello" at the end of the URL.
  73. The ``hello_world_page_handler()`` makes it possible for the users to access
  74. the actual page. Inside the function we first give an array of parameters to the
  75. ``elgg_view_layout()`` function.
  76. The parameters include:
  77. - The title of the page
  78. - The contents of the page
  79. - Filter which is left empty because there's currently nothing to filter
  80. This creates the basic layout for the page. The layout is then run through
  81. ``elgg_view_page()`` which assembles and outputs the full page.
  82. You can now go to the address http://www.mysite.com/hello/ and you should see the page.
  83. .. figure:: images/hello_world2.png
  84. :align: center
  85. :width: 60%
  86. Elgg is now routing the URL http://www.mysite.com/hello/ to the page you created.