0

I am learning about shortcodes. I've made 2 shortcodes that makes an accordion with bootstrap. One shortode (accordionGroup) creates the body that contains one or more shortcodes to make the items and it's content (accordionItem).

Now I need to create 2 different accordion. The problem is: I need a different id for each accordion and parse it to each item from that accordion. That is to make the accordion animation.

Here is the code

//This is the accordion container
function accordionGroup( $atts, $content = null )
{
    extract(shortcode_atts(array(
    'id_container' => '',
), $atts));
    return "<div class='panel-group' id=".$id_container." role='tablist' aria-multiselectable='true'>".do_shortcode($content)."</div>";
}
add_shortcode( 'accordion-group', 'accordionGroup');

//This is the accordion item. It's need a reference from the accordion ID
function accordionItem( $atts, $content = null)
{
    extract(shortcode_atts(array(
    'titulo' => '',
    'id' => '',
), $atts));

    return "
        <div class='panel panel-default'>
            <div class='panel-heading' role='tab' id=".$id.">
                <h4 class='panel-title'>
                    <a class='collapsed' role='button' data-toggle='collapse' data-parent='#the-reference-goes-here' href='#".$id."-collapse' aria-expanded='false' aria-controls=".$id."-collapse'>".$titulo."</a>
                </h4>
            </div>
            <div id='".$id."-collapse' class='panel-collapse collapse' role='tabpanel' aria-labelledby=".$id.">
                <div class='panel-body'>".$content."</div>
            </div>
        </div>";
}
add_shortcode( 'accordion-item', 'accordionItem');

2 Answers 2

1

You could pass the variable via a global but it would be neater to trap it in a static variable:

function track_acc_id($new_id = false) {
  static $id;
  if (!empty($new_id)) {
    $id = $new_id;
  }
  return $id;
}

//This is the accordion container
function accordionGroup( $atts, $content = null )
{
    extract(shortcode_atts(array(
    'id_container' => '',
), $atts));

    track_acc_id($id_container);

    return "<div class='panel-group' id=".$id_container." role='tablist' aria-multiselectable='true'>".do_shortcode($content)."</div>";
}
add_shortcode( 'accordion-group', 'accordionGroup');

//This is the accordion item. It's need a reference from the accordion ID
function accordionItem( $atts, $content = null)
{
    extract(shortcode_atts(array(
    'titulo' => '',
    'id' => '',
), $atts));

    $id = track_acc_id();

    return "
        <div class='panel panel-default'>
            <div class='panel-heading' role='tab' id=".$id.">
                <h4 class='panel-title'>
                    <a class='collapsed' role='button' data-toggle='collapse' data-parent='#the-reference-goes-here' href='#".$id."-collapse' aria-expanded='false' aria-controls=".$id."-collapse'>".$titulo."</a>
                </h4>
            </div>
            <div id='".$id."-collapse' class='panel-collapse collapse' role='tabpanel' aria-labelledby=".$id.">
                <div class='panel-body'>".$content."</div>
            </div>
        </div>";
}
add_shortcode( 'accordion-item', 'accordionItem');

echo do_shortcode('[accordion-group id_container="d00d"]');
echo do_shortcode('[accordion-item]');

Or the whole thing into a class.

4
  • Your answer would be more useful if you remove extract(). ;-) Commented Oct 26, 2015 at 14:25
  • 1
    My answer would be more useful if I a did a number of things to clean up that code, or refactor completely, but I have limited time. Commented Oct 26, 2015 at 14:31
  • No problem, understood :-) Commented Oct 26, 2015 at 14:33
  • I'm am wondering if it is even too complex in principle and the same results can be achieved with much less markup. Commented Oct 26, 2015 at 14:36
0

I have just read your code and is this correct

$id_container 

instead

$atts['id_container']

And may be this code error for you. Can you check once this?

1
  • The 'data-toggle' inside the accordionItem function needs to point to the accordion ID that contain it. All I need is a way to get the ID from accordionGroup function and pass it into accordionItem. For example, if the accordion has id="animation" (accordionGroup), the items from that accordion (accordionItem) must have data-toogle="#animation". If another accordion has id="videogames", the items from that accordion must have datat-toggle="#videogames", and so on. Commented Oct 26, 2015 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.