WordPress - Using shortcodes


You can easily register and use shortcodes for WordPress using Outlast Framework. The actual syntax is no different from the standard WordPress way, but we like to use reroute so that the actual execution is separated into the standard MVC-style coding of Outlast Framework.

What are short codes

Short codes are a way to include dynamic content within a WordPress article. Say you want to include a dynamic gallery in the middle of a post – well, you could register the sortcode ‘nicegallery’ and simply add [nicegallery] to your post. The gallery would show up in place of this short code, embeded into the middle of the post.

Registering short codes

You would usually register shortcodes in the __wpload function of your default controller. The parameters are similar to other registration methods. In Outlast Framework’s implementation, one function is allowed to be registered per shortcode so registering another will overwrite the previous.

// Register short code
$this->zajlib->wordpress->register_shortcode(array(
  'code'=>'nicegallery',
  'reroute'=>'/widgets/nicegallery/',
));

You can also register shortcodes to display templates.

// Register short code
$this->zajlib->wordpress->register_shortcode(array(
  'code'=>'nicegallery',
  'template'=>'widget/nicegallery.html',
));

Displaying short codes

When the short code is fired, execution is rerouted to the controller (or template) set above. So /widgets/nicegallery/ would now take over and display some content:

// widgets.ctl.php
function nicegallery($attrs){
  return "A beautiful gallery goes here!";
}

IMPORTANT! content must be returned and not printed, and must be the 2nd and 3th parameter: “false, true”;

// something.ctl.php
function showthis(){
  return $this->zajlib->template->show('showthis.html', false, true);
}

In html templates you can use {{wp_post.content|wp_do_shortcode}} (you need the filter for shortcode processing) or you can use {% wp_the_content %} which processes shortcodes automatically.

Passing parameters to short codes

You can also pass parameters to short codes. An example: [nicegallery id="123" size="medium"]

These parameters would be passed as attributes to the reroute handler. So:

// widgets.ctl.php
function nicegallery($attrs){
  $id = $attrs['id'];
  $size = $attrs['size'];
  return "A beautiful gallery ($id) goes here with $size sized photos!";
}

Shortcodes with open and closing tags

You can also use shortcodes as open/closing tags:

Here are some words within the WordPress content.

[processthis]This is the content to be processed.[/processthis]

Here are some more words.

This can be useful when a block of text needs to be run through a shortcode to process it. The registration will be exactly the same as above, but the reroute handler function will take some more parameters:

// widgets.ctl.php
function processthis($attrs, $content, $tag){
  // $attrs are the attributes passed, $content is the content between the tags, $tag is the name of the current shortcode
    // Now do something here to process $content!
  return "This the content that has been processed.";
}

WordPress gallery shortcode

Outlast Web & Mobile Development (c) 2023 | Privacy Policy |