WordPress.org

Ready to get started?Download WordPress

Forums

Arconix Shortcodes
[resolved] Feature Req: Nesting Toggles, Nesting Accordions, Specify Open/Closed on Load? (7 posts)

  1. MGmirkin
    Member
    Posted 1 year ago #

    So, I want to create a nested situation where I have one toggle show/hide sub-toggles. Or, likewise, have one accordion hide another accordion.

    For instance:

    [toggle heading="H1" title="Essential Guide to the Electric Universe"]

    [toggle heading="H2" title="Preface"]Text[/toggle]

    [toggle heading="H2" title="Introduction"]Text[/toggle]

    [/toggle]

    This doesn't appear to work correctly, as-is.

    There is a single gray box However, rather than a single white toggle "Essential Guide..." with two child toggles, there are two toggles in the gray box. "Essential Guide" and "Introduction." "Introduction" toggles properly with "Text" inside it. However, I'd think that it should be hidden within "Essential Guide" and only displayed with the primary toggle is toggled open. When one toggles "Essential Guide..." it open to reveal "Preface" and the word "Text" below the "Preface" toggle, not inside it. When you toggle "Preface" there is nothing "inside" it.

    If we could make nesting work, this would potentially be a rather useful plugin. I think a similar situation exists for the Accordion shortcode as well. It would be nice if one could nest accordions as well... Though for my purposes, Toggle would probably be the more correct choice as accordion seems to always need to have one sub-text open, whereas Toggle, I think, doesn't, thus one could collapse all the toggles.

    While we're at it, if we could have some kind of a switch in the shortcode to specify whether a given toggle is "open or closed" upon page load (default to 'closed,' if unspecified, or give an option in the settings as to how to handle unspecified open/closed state by default), that would be cool too. And in handling the open/closed initial state on load, it would be nice if higher level toggle shortcodes were honored. That is, if we have something like:

    [toggle state="closed" title="Closed Parent"]

    [toggle StartState="open" title="Child 1 Open"]Text[/toggle]
    [toggle StartState="closed" title="Child 2 Closed"]Text[/toggle]

    [/toggle]

    The child 1 should be "open" but hidden inside the closed parent. That is, once the parent is opened we see child 2 is closed & child 1 is open already. But, we don't see that until we toggle to open the Parent.

    Make sense? Is that possible? I think that would make this pretty useful for things like FAQs or for displaying chapters/subsections of "books"/"papers" in a collapsible format, etc.?

    http://wordpress.org/extend/plugins/arconix-shortcodes/

  2. MGmirkin
    Member
    Posted 1 year ago #

    I come to understand that Shortcodes can't be nested in WP??

    http://bringthepixel.com/support/topic/toggle-within-a-toggle

    Is there any way around this? Do you know of any plugins that support nested toggling? It seems silly/stupid that such a robust CMS would have such a silly flaw built in to it...

  3. John Gardner
    Member
    Plugin Author

    Posted 1 year ago #

    Hi MGmirkin,

    Thanks for using my plugin. I do have an option for setting the default state of the toggle on my roadmap for inclusion in the next version of the plugin.

    To your primary issue, based on my testing it would appear some shortcodes of the same type can't be nested within each other without causing trouble, and in this case it seems toggle is one of those shortcodes.

    But for the record, shortcodes can definitely be nested. See http://test.arconixpc.com/accordions/ for an example.

    The only way you might get around this issue is to use a combination of toggles and accordions.

  4. MGmirkin
    Member
    Posted 1 year ago #

    Sorry, should have updated after some further investigation. More specifically, shortcodes of the SAME NAME cannot be nested. It's a known issue they haven't dealt with yet, and maybe never will...? :o\

    There is a workaround, I think, but they seem to have offloaded the work on to plugin developers rather than fix the core issue...

    http://codex.wordpress.org/Shortcode_API

    The shortcode parser correctly deals with nested shortcode macros, provided their handler functions support it by recursively calling do_shortcode():

    [tag-a]

    [tag-b]

    [tag-c]

    [/tag-b]

    [/tag-a]

    However the parser will fail if a shortcode macro is used to enclose another macro of the same name:

    [tag-a]

    [tag-a]
    [/tag-a]

    [/tag-a]

    This is a limitation of the context-free regexp parser used by do_shortcode() - it is very fast but does not count levels of nesting, so it can't match each opening tag with its correct closing tag in these cases.

    So, I don't know whether that's a possibility (counting nesting levels and in some way modifying how the shortcodes are passed to the core, so that they're not named the same thing?), until/unless they fix the core issue with not counting nesting levels...?

    Not sure I've 100% understood their explanation. But, hopefully that pens it in?

    So, maybe it would take the above example from the OP and somehow output something like:

    [toggle1 title="Essential Guide"]

    [toggle2 title="Preface"]

    [toggle4 title="About This Guide"]
    Text
    [/toggle]

    [toggle5 title="Acknowledgments"]
    Text
    [/toggle]

    [/toggle]

    [toggle3 title="Introduction"]
    Text
    [/toggle]

    [/toggle]

    Best,
    ~MG

  5. John Gardner
    Member
    Plugin Author

    Posted 1 year ago #

    Hopefully I can explain it in a little simpler terms.

    When you are adding content in the editor window, you enter the shortcode name, e.g. [toggle]. On the backend, WordPress runs the content through a parser (in this case, it's part of a WP function called do_shortcode();) that translates the shortcode into html that can be displayed on screen.

    The long-term fix for handling nested shortcodes of the same name has to come out of WordPress core, unless a developer with an excellent handle on regex (not I) writes his own plugin.

    You could certainly add multiple toggle shortcodes which do the same thing just with a different name, I'm not 100% sure how the parser handles that. You'd need as many shortcodes as the number of levels deep you'd wish to go.

  6. MGmirkin
    Member
    Posted 1 year ago #

    Yeah, I started a thread over in the suggestions / feedback area after a little poking around...

    http://wordpress.org/support/topic/nesting-shortcodes-doesnt-work-as-expected

    I've been going back and forth slightly with one moderator on whether or not it's worthwhile to fix the core versus cobbling together ad hoc solutions on a case-by-case basis...

    I'm of the opinion that fixing the core would be better, he seemed to think that a "band-aid" approach was sufficient. But I still politely disagreed... Devil's Advocacy & all. ;)

    If I were a programmer and knew what the issues were, I'd take a crack at it. But I'm not & I don't fully... =o\ So, must leave it to those who do, I suppose.

    Thx,
    ~MG

  7. MGmirkin
    Member
    Posted 1 year ago #

    You could certainly add multiple toggle shortcodes which do the same thing just with a different name, I'm not 100% sure how the parser handles that. You'd need as many shortcodes as the number of levels deep you'd wish to go.

    Ahh, I'd been wondering about that after the mod on that thread had made some mention of a kludge to fix it ad hoc. Even asked exactly that. "Can you alternate back and forth between two differently named shortcodes (so long as one is not directly inside of another with the precisely same name) or would you have to define arbitrarily many shortcodes with completely different names for however arbitrarily deep you wanted the nesting hierarchy to go?"

    Hadn't heard back on that yet. But thx, sounds like the even kludgier kludge is the correct answer, making the problem that much more complicated and more necessary to fix.

    Now, granted, maybe in reality you only need a maximum of say 4-5 levels deep. But still, it seems like it would make more sense to fix it all in one place (regexp or what have you) than to have to add multiple kludges on top of kludges across a thousand sites that want to systematically use nesting in reasonable ways like hierarchical toggles, etc.

    But, anyway, the regexp issue isn't you fault, so I won't belabor the nuisance any further hereabouts. Unless there's some easy way around it you can think of for adding nesting to your plugin. ^_^

    Or unless you really wanted to get involved in advocating for fixing the core to better support nesting to make plugin programmers' lives easier. But I won't ask you to do that. Unless ya' wanna'. No pressure, tho'. ;)

    Cheers & have a good one! Appreciate the chat & chance to vent about WP core issues.

    ~MG

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic