Wordpress shortcodes are a great way of allowing content to be managed more easily by any web administrator / blogger.
Using the Fishpig WordPress Integration extension this becomes possible in Magento. Here is a quick tutorial on how to achieve this.
Step 1. Define the module
app/etc/modules/Limely_FishpigShortcode.xml
true
local
This file adds the Limely_FishpigShortcode module into Magento’s architecture.
Step 2. Initialise the module configuration
app/code/local/Limely/FishpigShortcode/etc/config.xml
0.0.1
Limely_FishpigShortcode_Model
Limely_FishpigShortcode_Helper
fishpigshortcode/observer
applyShortcodes
singleton
This file gives instructions to Magento, it attaches listeners to the “wordpress_shortcode_apply” event that is triggered when rendering a post from within the Fishpig_Wordpress module.
Step 3. Create an empty helper class
app/code/local/Limely/FishpigShortcode/Helper/Data.php
For the purpose of this demonstration we are going to leave this file blank, unfortunately as we have registered a helper in the module configuration, this file needs to be created regardless of whether it contains any functionality or not. [gap height="50"]Step 4. Create the shortcode helper
[lead]app/code/local/Limely/FishpigShortcode/Helper/Shortcode/Bold.php[/lead] [cc lang="php"] _getShortcodes($content)) == false) {
return;
}
foreach ($shortodes as $shortcode) {//example shortcode [bold class=”intro”]Hello world.[/bold]
//this is how you get the inner content of the shortcode
$innerContent = $shortcode->getInnerContent();//get the shortcode parameters
$params = $shortcode->getParams();//this is how you access the class parameter
$class = $params->getClass();//i’ve create a block here to show what is possible, you could do what I’m trying to achieve without a template in this instance
$block = $this->_createBlock(‘core/template’)->setData(array(‘class’ => $class, ‘inner_content’ => $innerContent))->setTemplate(‘limely/shortcodes/bold.phtml’);$content = str_replace($shortcode->getHtml(), $block->toHtml(), $content);
}return $content;
}/**
* Return bold shortcode tag
*
* @return string
*/
public function getTag() {
return ‘bold’;
}}
Step 5. Create the shortcode template
app/design/frontend/{{theme_name}}/default/limely/shortcodes/bold.phtml
getClass()) : ?>