yii-shortcode Create WordPress like shortcodes

Yii-shortcode is an extension for the Yii PHP framework that provides ability to create WordPress like shortcodes.

Usage

Setup

Download the latest release from Yii extensions.

Unzip the extension under protected/extensions and add the following to your application config:

array(
  'components' => array(
    'shortcode' => array(
        'class' => 'application.extensions.yii-shortcode.ShortCode',
     ),
  ...
  ...
  ),
);

protected/config/main.php

Registering Shortcodes

You can register as many shortcodes as you want in base controller or in any custom controller as shown below:

public function init()
{
	Yii::app()->shortcode->add_shortcode('gallery', array($this, 'gallery_callback'));
	Yii::app()->shortcode->add_shortcode('caption', array($this, 'caption_callback'));
	return parent::init();
}
  
/**
 * If there are registered shortcodes, this method will replace them with their handler output.
 * @param string $output the output generated by the current action.
 * @return string the output that has been processed by shortcode parser.
 */
public function processOutput($output)
{
	$output = Yii::app()->shortcode->parse($output);
	return parent::processOutput($output);
}

protected/components/Controller.php

/**
*
* Shortcode handler for the shortcode [gallary id=value]
* @param array $atts associative array of attributes, or empty string if no attributes are passed
* @return string the output generated for the shortcode
*/
function gallery_callback($atts)
{
	extract( Yii::app()->shortcode->shortcode_atts( array(
		'id' => '',
	), $atts ) );
	return "<div id='gallery'>Gallery #{$id}</div>";
} 


/**
*
* Shortcode handler for the shortcode [caption]My Caption[/caption]
* @param array $atts associative array of attributes, or empty string if no attributes are passed
* @param string $content - the enclosed content (if the shortcode is used in its enclosing form)
* @return string the output generated for the shortcode
*/

function caption_callback($atts, $content)
{
	return '<span class="caption">' . $content . '</span>';
}   

protected/controllers/SiteController.php

Overview

The add_shortcode() function is used to register a shortcode handler. It takes two parameters: the shortcode name (the string used in a post body), and the callback function name.

Three parameters are passed to the shortcode callback function. You can choose to use any number of them including none of them.

  • $atts - an associative array of attributes, or an empty string if no attributes are given
  • $content - the enclosed content (if the shortcode is used in its enclosing form)
  • $tag - the shortcode tag, useful for shared callback functions
Output

The return value of a shortcode handler function is inserted into the page in place of the shortcode macro. Remember to use return and not echo - anything that is echoed will be output to the browser, but it won't appear in the correct place on the page.

When used like this:

[caption]My Caption[/caption]

The output would be:

<span class="caption">My Caption</span>
5 0
6 followers
433 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Others
Developed by: Tahir Yasin
Created on: Feb 21, 2014
Last updated: 4 years ago

Downloads

show all

Related Extensions