Welcome Guest, Not a member yet? Register   Sign In
shortcodes for ci4
#1

(This post was last modified: 10-07-2023, 07:29 AM by xsPurX.)

I had one that worked. This might be a php issue. The $shortcode_array() comes back as undefined even though I am passing information to it. Any ideas on why it is returning undefined?

PHP Code:
    public function parse ($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($shortcode0strpos($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/shortcodes/') . '/';
            
            
foreach ($shortcode_array as $search => $shortcode) {
                $shortcode_model $shortcode['code'];
                if (file_exists($path $shortcode_model '.php') && is_file($path $shortcode_model '.php')) {
                    $this->_ci->load->model('shortcodes/' $shortcode_model);
                    $str str_replace($search$this->_ci->$shortcode_model->run($shortcode['params']), $str);
                }
            
            
}
        }
        
        
// Return the entire parsed string
        return $str;
    
Reply
#2

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($shortcode0strpos($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. Big Grin
Reply
#3

(This post was last modified: 04-19-2024, 09:40 AM by xsPurX.)

Oddly I put this on a new server and its not loading my shortcodes. Is there a php setting that would cause this to break?  I have it working on another host, but on a fresh server setup it is not loading the model files, or parsing the code. Any help would be appreicated.

EDIT: Nevermind, it wasn't working cause I was using shortcode with a lowercase like [ai:grid] when the model is Grid.php I guess the other host I use didn't have case sensitivity.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB