WordPress Shortcodes (Possibly Nested & Input) Code Answer

I’ve been battling with this problem in WordPress particularly with using one shortcode which has a nested shortcode inside it, which works like a charm, however I wanted to make it a simple shortcode which in turn then would be easier for someone to use.

You can see the code here

function pb_timeline($attr,$content){
        $content = wpautop(trim($content));
        return '<ul class="timeline">'.do_shortcode($content).'</ul>';

function pb_tlsection($attr,$content=null){
          return '<li><div class="year">YYYY</div><div class="dot"></div><div class="box"><p>Description To Go Here.</p></div></li>';

The HTML output for the shortcode is basically this

<ul class="timeline">
      <div class="year">YYYY</div>
      <div class="dot"></div>
      <div class="box">Description</div>

What I would like to do is to use a WordPress shortcode like

[timeline_section year="2013" description="Hello"]

The Year and Description could be filled out by the content editor for example. I have the shortcode working for this, but I have no idea or anywhere to start regarding inputting the data from the shortcode tag to display exactly what it should be.

Edit: Adding the solution to this for reference to whoever comes across the same problem themselves. Improving on Cyrille’s answer which has set me on the right path. I have found out that the following is more effective as it is…

function pb_tlsection($atts){
        extract( shortcode_atts( array(
            'year' => 'YYYY',
            'desc' => 'Description',
            ), $atts, 'timeline_section' ) );
          return '<li><div class="year">' . $year . '</div><div class="dot"></div><div class="box"><p>' . $desc . '</p></div></li>';


You should have a look at the following link: http://codex.wordpress.org/Shortcode_API Particularly at the extract function.

You could do for instance:

 function pb_tlsection($attr,$content=null){
        extract( shortcode_atts( array(
            'year' => '2013',
            'description' => 'Hellow',
        ), $atts ) );

        return '<li><div class="year">'.$year.'</div><div class="dot"></div><div class="box"><p>'.$description.'</p></div></li>';


Related Posts

© No Copyrights, All Questions are retrived from public domain.
Tutorial Guruji