With some changes i managed to get this to work. You just need to put the model of your shortcode into the models folder. Below is a quick example.
First in common.php I have this function
PHP Code:
function parse_shortcode($str)
{
// Check for existing shortcodes
if (! strstr($str, '[ai:')) {
return $str;
}
// Find matches against shortcodes like [ai:user id=1|class=admin]
preg_match_all('/\[ai:([a-zA-Z0-9-_: |=\.]+)]/', $str, $shortcodes);
if ($shortcodes == NULL) {
return $str;
}
// Tidy up
foreach ($shortcodes[1] as $key => $shortcode) {
if (strstr($shortcode, ' ')) {
$code = substr($shortcode, 0, strpos($shortcode, ' '));
$tmp = explode('|', str_replace($code . ' ', '', $shortcode));
$params = array();
if (count($tmp)) {
foreach ($tmp as $param) {
$pair = explode('=', $param);
$params[$pair[0]] = $pair[1];
}
}
$array = array('code' => $code, 'params' => $params);
}
else {
$array = array('code' => $shortcode, 'params' => array());
}
$shortcode_array[$shortcodes[0][$key]] = $array;
}
// Replace shortcode instances with HTML strings
if (count($shortcode_array)) {
// Find the appropriate model for every shortcode, load it and call $model->run($params)
$path = realpath(APPPATH.'/Models/') . '/';
foreach ($shortcode_array as $search => $shortcode) {
$shortcode_model = $shortcode['code'];
if (file_exists($path . $shortcode_model . '.php') && is_file($path . $shortcode_model . '.php')) {
$shortcode_model = model($shortcode_model);
//load->model('shortcodes/' . $shortcode_model);
$str = str_replace($search, $shortcode_model->run_short($shortcode['params']), $str);
}
}
}
// Return the entire parsed string
return $str;
}
Now if I use my string in my page database
[ai:nav id=6|theme=navbar-default|pos=navbar-fixed-top|type=bar]
I run parse_shortcode() over this string which loads the Nav.php model
Right now I have nothing in it, You can define as many parameters as you like.
PHP Code:
<?php
namespace App\Models;
use CodeIgniter\Model;
class Nav extends Model
{
public function run_short($params = array())
{
$id = isset($params['id']) ? $params['id'] : '0';
$theme = isset($params['theme']) ? $params['theme'] : 'navbar-default';
$pos = isset($params['pos']) ? $params['pos'] : '';
$type = isset($params['type']) ? $params['type'] : 'bar'; //bar or pills
$str = 'replaced';
return $str;
}
}
You can even throw some database code to pull whatever you like from the database.
I hope this helps someone else get shortcodes. Cause they are awesome.