Magento: How to use WordPress Shortcodes in Fishpig

14 September 2016| Post by Graham

AuthorGraham

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

[cc lang=”xml”]



true
local



[/cc]

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

[cc lang=”xml”]



0.0.1





Limely_FishpigShortcode_Model




Limely_FishpigShortcode_Helper






fishpigshortcode/observer
applyShortcodes
singleton






[/cc]

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

[cc lang=”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.

Step 4. Create the shortcode helper

app/code/local/Limely/FishpigShortcode/Helper/Shortcode/Bold.php

[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’;
}

}
[/cc]

Step 5. Create the shortcode template

app/design/frontend/{{theme_name}}/default/limely/shortcodes/bold.phtml

[cc lang=”php”] getClass()) : ?>

Step 6. Create the event listener

app/code/local/Limely/FishpigShortcode/Model/Observer.php

[cc lang=”php”] getData(‘content’);

//get the post content
$content = $contentObj->getData(‘content’);

//apply the shortcode
Mage::helper(‘fishpigshortcode/shortcode_bold’)->apply($content, $observer->getData(‘object’));

//replace the existing content on content transporter object
$contentObj->setData(‘content’, $content);
}
}
[/cc]

This file listens to when the Fishpig default shortcodes are being applied, then applies the bold shortcode. You can apply multiple shortcodes in the “applyShortcodes” method if you like.

That’s it, just remember to flush your cache. Also, feel free to get in touch with me at gavin@limely.co.uk for any additional help or blog suggestions.

Scroll