最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Shortcode conflicts

programmeradmin1浏览0评论

My plugin provide shortcodes like this:

[cfgeo return="city"] - returns city name

[cfgeo include="us"]Text only seen in the US country[/cfgeo] - returns this text only from the visitors in the US.

If I place shortcodes in the content like this:

<div>[cfgeo return="city"]</div>

<div>[cfgeo include="us"]Text only seen in the US country[/cfgeo]</div>

WordPress made error and parse it like this:

<div>[cfgeo return="city"]</div><div>[cfgeo include="us"]Text only seen in the US country[/cfgeo]<div>

That mean all between first chortcode and closed state of the shortcodes are parsed like one big shortcode.

[cfgeo return="city"]EVERYTHING INSIDE[/cfgeo]

And that made a error.

How and do I can avoid this?

My plugin provide shortcodes like this:

[cfgeo return="city"] - returns city name

[cfgeo include="us"]Text only seen in the US country[/cfgeo] - returns this text only from the visitors in the US.

If I place shortcodes in the content like this:

<div>[cfgeo return="city"]</div>

<div>[cfgeo include="us"]Text only seen in the US country[/cfgeo]</div>

WordPress made error and parse it like this:

<div>[cfgeo return="city"]</div><div>[cfgeo include="us"]Text only seen in the US country[/cfgeo]<div>

That mean all between first chortcode and closed state of the shortcodes are parsed like one big shortcode.

[cfgeo return="city"]EVERYTHING INSIDE[/cfgeo]

And that made a error.

How and do I can avoid this?

Share Improve this question asked Aug 23, 2019 at 10:39 Ivijan Stefan StipićIvijan Stefan Stipić 4553 silver badges16 bronze badges 3
  • You’re using the same shortcode for very different things. They should be separate. – Jacob Peattie Commented Aug 23, 2019 at 10:43
  • Yes, before that was working, after 5.2 not works anymore. You are right about that but still thinking is there solution or I must add new shortcodes? – Ivijan Stefan Stipić Commented Aug 23, 2019 at 10:51
  • 1 You need a separate shortcode. Even as a human it's impossible to read your examples and know which shortcode [/cfgeo] is supposed to close. It's too ambiguous. Even if it did work, it's a bad idea. Trying to have a single shortcode that does everything is poor shortcode design. – Jacob Peattie Commented Aug 23, 2019 at 12:42
Add a comment  | 

1 Answer 1

Reset to default 1

When you define your shortcode, you might try one of a couple "if" statements to fix this issue, until you have time to create separate shortcodes.

function cfgeo_shortcode( $atts, $content = "" ) {
    if (!isset($content) || stristr($content,'cfgeo')!==FALSE){
        // do short shortcode stuff here

    } else {
        // do 'Container' shortcode stuff here

    }
    return $output;
}
add_shortcode( 'cfgeo', 'cfgeo_shortcode' );

I would also recommend ... when you create your replacement shortcodes, you should not only create separate codes, you should also avoid parameters with keywords like "return" and "include" for their respective names.

Good luck! Hope this was helpful.

发布评论

评论列表(0)

  1. 暂无评论