Shortcode plugins for own custom cms like wordpress shortcode plugins

两盒软妹~` 提交于 2020-01-15 04:01:08

问题


I have created a custom CMS for my own and a plugin system that is working as I expected.

Now I want to create plugins based on shortcodes, eg: [gallery attr1=100 attr2="string"] and [mycode title='message' color='#F00']Hello World![/mycode]

I want to handle the above mentioned shortcodes in my CMS. Functions will replace shortcodes with HTML and get & set attributes as parameters for mySQL queries or something else.

may be regular expressions will help, I am not expert in regular expressions. I would don't like to use such regular expressions if there are other smiler or good ways exist .

CMS developed using PHP and mySQL.

I have visited the wordpress developers sites and got the concepts, I have already created functions that register or set plugins, menus, theme sidebars etc.

I think this info is enough to get the point as I can just explain in this way.

thank you in advance


回答1:


This code solved the logic

function plugin_sortcode($contents) {
    global $SORTCODES, $DATA;

    foreach ($SORTCODES as $name => $fn) {
        $attr = array();
        preg_match_all("/\[" . $name . " (.*?)\]/", $contents, $matches);
        if ($matches[0]) {

            $code = $matches[0][0];
            if (isset($matches[1])) {
                $attrs = $matches[1][0];
                $attrs = explode(" ", $attrs);
                foreach ($attrs as $values) {
                    $attrs1 = explode("=", $values);
                    $attr[$attrs1[0]] = $attrs1[1];
                }
            }

            $data = $fn($attr, $DATA);
            $contents = str_replace($code, $data, $contents);
        }
    }

    return $contents;
}


来源:https://stackoverflow.com/questions/20237924/shortcode-plugins-for-own-custom-cms-like-wordpress-shortcode-plugins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!