custom-content-shortcode icon indicating copy to clipboard operation
custom-content-shortcode copied to clipboard

[if check...] fails when shortcode parameter not passed

Open codemonkeynorth opened this issue 5 years ago • 1 comments

Hi @eliot-akira,

great plugin but I'm having to rely on a few workarounds for conditionals currently.

this is related to https://github.com/eliot-akira/custom-content-shortcode/issues/10

I need to be able to show some html when a shortcode parameter is not passed. in part because there's no [if]...[elseif]...[elseif]...[else]...[/if] structure that i can see?

anyway.. given the following two scenarios eg

[myshortcode param="foo"] [myshortcode]

and the following meta shortcode logic:

[if check={PARAM} value="foo"]Foo[/if]
[if check={PARAM} value="bar"]Bar[/if]
<!-- this doesn't work -->
[if check={PARAM} value=""]Baz[/if]

I need the second shortcode to output "Baz" when no param is passed

but this doesn't work either (see other issue)

[if exists]{PARAM}
[show]param was passed
[else]param wasn't passed
[/if]

if param is not passed as an empty string like [myshortcode param=""] then {PARAM} inside the [if...] will just be the string "{PARAM}" and therefore exists and breaks the condition ie instead of it being empty it has the content {PARAM}.

I have a workaround but there's a bug if param value sent does not contain a space:

[if check={PARAM} value="{PARAM}"]
param not passed
[else]
{PARAM} 
[/if]

using this code... [myshortcode] shows "param not passed" (expected) [myshortcode param="foo"] shows “param not passed” (fail, should show "foo") [myshortcode param="foo bar"] shows “foo bar” (expected)

in all instances:

  1. if not passed, ideally {PARAM} should resolve to an empty string or maybe value=empty (or null)? (ideally something like [if check={PARAM} value=null]
  2. ..or an alternative syntax to determine if something isn't passed eg [if not check={PARAM}] etc
  3. if passed, param should parse accordingly to the correct value.. mostly it does except in that [if check] as explained

thanks for any input

regards J

codemonkeynorth avatar Sep 16 '20 12:09 codemonkeynorth

found another temporary workaround, for anybody awaiting a fix:

[myshortcode] [myshortcode param="foo"]

[if exists][format split="{}" part=2]{PARAM}[/format][show]
param was not passed
[else]
param was passed as {PARAM}
[/if]

what the format split does is create the literal string "PARAM" if no param is passed, or creates a blank string if a param was passed, since there are no {} brackets to split on.

codemonkeynorth avatar Sep 16 '20 13:09 codemonkeynorth