0
votes

Is it possible from nested shortcode to call its parent shortcode?

To illustrate my idea take into account this example:

[check-auth]
    [has-auth]Hello, dear Username[/has-auth]
    [no-auth]Please login here[/no-auth]
[/check-auth]

The idea is to check authorization in parent shortcode check-auth and then to use this information inside nested shortcodes has-auth and no-auth.

I do also understand that if this would be possible it means that we are binding together these shortcodes so that you cannot use has-auth outside check-auth (actually you can write has-auth so that is will handle this case too), and that this is not a good design desicion but still it would allow to write complex shortcodes with logic :)

1
The only way to do so that I see right now is to manually query nested shortcode inside $content and then to call do_shortcode($parsedShortcode). - chestozo
Or to use global php variables which is a bad practice but it works. - chestozo

1 Answers

0
votes

The right way is to use class for a shortcode that has nested shortcodes. So that from the nested shortcode you can always get access to some $this->property or call an $this->instanceMethod().

Basic example of a shortcode defined in class you can find here https://codex.wordpress.org/Function_Reference/add_shortcode#Examples.