How to include PHP commands in Section/Content Description (for Joomla 1.5.x)?
Written by kksou   
Tuesday, 22 April 2008

The DirectPHP plugin which I released in March 2008 allows you to include PHP commands in content items only.

This article shows you how to include PHP commands right inside Section/Category description.

You can see a live example here.

Solutions

  1. Download and install. First you need to download and install the plugin DirectPHP for Section/Category Description.

  2. Publish the plugin. Go to Extensions menu, select Plugin Manager and make sure the plugin is enabled.
  3. Add Trigger for Category Description. In the original Joomla installation, there is no trigger during the display of Category description. When there is no trigger, none of the mambot or plugin gets called. In order for Joomla to call the new plugin that we have just installed to include a module, we need to add a trigger.
  4. Note: If you've already installed the plugin loadmodule for Section/Category Description, you can skip this step.
    The same trigger will trigger both plugins.

    1. Go to the folder <Joomla root folder>/components/com_content/views/category, you will see a file called view.html.php.
    2. Open this file view.html.php in your favorite editor.
    3. Go to around Line 39. In the function display(), add the four lines highlighted in yellow near the beginning of the function as follows:
    4. class ContentViewCategory extends ContentView
      {
          function display($tpl = null)
          {
              global $mainframe, $option;
      
              // Initialize some variables
              $user		=& JFactory::getUser();
              $uri 		=& JFactory::getURI();
              $document	=& JFactory::getDocument();
              $pathway	=& $mainframe->getPathway();
      
              $category	=& $this->get('Category');
              $dispatcher	=& JDispatcher::getInstance();
              JPluginHelper::importPlugin('content');
              $results = $dispatcher->trigger('onBeforeDisplayTitle', array (& $category));
      
              // Get the menu item object
              $menus = &JSite::getMenu();
              $menu  = $menus->getActive();
      
    5. Save and close this file.

  5. Add Trigger for Section Description. If you also want to include modules for Section Description, you also need to add a trigger as explained above.
    1. Go to the folder <Joomla root folder>/components/com_content/views/section, you will see a file called view.html.php.
    2. Open this file view.html.php in your favorite editor.
    3. Go to around Line 36. In the function display(), add the four lines highlighted in yellow near the beginning of the function as follows:
    4. class ContentViewSection extends ContentView
      {
          function display($tpl = null)
          {
              global $mainframe, $option;
      
              // Initialize some variables
              $user		=& JFactory::getUser();
              $document	=& JFactory::getDocument();
      
              $section	=& $this->get('Section');
              $dispatcher	=& JDispatcher::getInstance();
              JPluginHelper::importPlugin('content');
              $results = $dispatcher->trigger('onBeforeDisplayTitle', array (& $section));
      
              // Get the page/component configuration
              $params = &$mainframe->getParams();
      
              // Request variables
              $limit = JRequest::getVar('limit', $params->get('display_num'), '', 'int');
              $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
      
    5. Then go to around Line 87, also in the function display(), add the three lines highlighted in yellow as follows. (Note: this one takes care of the case in which you have modules embedded in each of the category description):
    6. class ContentViewSection extends ContentView
      {
          function display($tpl = null)
          {
                  .
                  .
                  .
      
              //add alternate feed link
              if($params->get('show_feed_link', 1) == 1)
              {
                  $link	= '&format=feed&limitstart=';
                  $attribs = array('type' => 'application/rss+xml', 'title' => 'RSS 2.0');
                  $document->addHeadLink(JRoute::_($link.'&type=rss'), 
                      'alternate', 'rel', $attribs);
                  $attribs = array('type' => 'application/atom+xml', 'title' => 'Atom 1.0');
                  $document->addHeadLink(JRoute::_($link.'&type=atom'), 
                      'alternate', 'rel', $attribs);
              }
      
              $dispatcher	=& JDispatcher::getInstance();
              JPluginHelper::importPlugin('content');
      
              for($i = 0; $i < count($categories); $i++)
              {
                  $category =& $categories[$i];
                  $category->link = JRoute::_('index.php?view=category&id='.
                      $category->slug);
      
                  $results=$dispatcher->trigger('onBeforeDisplayTitle', array(&$category));
              }
      
              if ($total == 0) {
                  $params->set('show_categories', false);
              }
      
    7. Save and close this file.

  6. That's it! You can now include PHP commands right inside the Section/Category description!
  7. To try if it works, edit any section or category description, and copy the following text into the description:
  8. Current date and time is: <?php echo date('Y-m-d H:i:s');?>

    You should see the current date and time displayed in the section or category description.

  9. There are more examples in DirectPHP plugin. You will also find explanations on how to customize the block list.

Very Important Note about the Editor!!!

If you're using the default TinyMCE WYSIWYG Editor, you will face one problem. When you first enter PHP commands into the editor, encapsulated in <?php ... ?>, everything's ok. But as soon as you click the save button, all your PHP commands will disappear!

Don't worry. They are all still there. But the problem is, unlike content items, the default TinyMCE WYSIWYG Editor refuses to display anything in tags.

There are two solutions to this.

  1. Go to Site - Global Configuration, and set Default WYSIWYG Editor to "No WYSIWYG Editor", and edit everything in plain HTML and PHP codes.
  2. Install another editor such as JoomlaFCK that allows you to easily switch between WYSIWYG mode and Source Code mode.


User reviews

There are no user reviews yet.

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Copyright © 2006-2008. kksou.com. All Rights Reserved