Search code examples
joomlajoomla3.0joomla-extensions

Create simple Shortcode Plugin Joomla


I use onContentPrepare event to change this [test] to other text o prinf html like wordpress shortcodes but nothing change.

What is wrong?

This is the shortcodejd.xml

<?xml version="1.0" encoding="utf-8"?>
    <extension version="3.1" type="plugin" group="search">
        <name>shortcodejd</name>
        <author>Joomla! Project</author>
        <creationDate>November 2005</creationDate>
        <copyright>Copyright (C) 2005 - 2013 Open Source Matters. All rights reserved.</copyright>
        <license>GNU General Public License version 2 or later; see LICENSE.txt</license>
        <authorEmail>[email protected]</authorEmail>
        <authorUrl>www.joomla.org</authorUrl>
        <version>3.1.0</version>
        <description>SHORTCODEJD</description>
        <files>
            <filename plugin="shortcodejd">shortcodejd.php</filename>
            <filename>index.html</filename>
        </files>
        <languages>
            <language tag="en-GB">en-GB.shortcodejd.ini</language>
            <language tag="en-GB">en-GB.shortcodejd.sys.ini</language>
        </languages>
        <config>

        </config>
    </extension>

and this shortcodejd.php

defined('_JEXEC') or die;


class PlgContentShortcodejd extends JPlugin
{
 protected $autoloadLanguage = true;


    public function onContentPrepare($context, &$article, &$params, $limitstart)
    {
        $article->text = str_replace("[test]","<h1>Hi</h1>",$row->article);
        return true;
    }
}

Solution

  • In this function

    public function onContentPrepare($context, &$article, &$params, $limitstart)
        {
            $article->text = str_replace("[test]","<h1>Hi</h1>",$row->article);
            return true;
        }
    

    Where did you get this $row from. Instead just use $article->text. Your function should look like

    public function onContentPrepare($context, &$article, &$params, $limitstart)
            {
                $article->text = str_replace("[test]","<h1>Hi</h1>",$article->text);
                return true;
            }
    

    Also as @Yoleth pointed out you need to have a content plugin rather than a search plugin as you are going to replace in contents.