0
votes

I created my first custom shortcode in my theme, adding the following lines in functions.php

function ec_toggle_content($atts, $content = null){

   $togtit='Title test';

   extract(shortcode_atts(array('togtit'=> ''), $atts));

   $output .= '<h4 class="toggletrg">'.$title.' <i class="fa fa-plus right toggleicon"></i><i class="fa fa-minus right toggleicon hidden"></i></h4>';
   $output .= '<div class="togglecont">';
   $output .= do_shortcode($content);
   $output .= '</div>';
   return $output;

   add_shortcode('toggle', 'ec_toggle_content');
}

and when I edit one of my posts adding the shortcode i.e.

[toggle togtit="comn pleas work"]bla bla bla text[/toggle]

It just renders as plain text... (for reference, http://ipfcommunity.wdemo.it/aggiornamenti/news/respirare-sottacqua-piccoli-esercizi-sopravvivenza-spirituale/)

I really can't figure out how to make it work, I read that the most common problem could be that I don't render using get_content() to retrieve the post content, but that's what I do so I can't find any further advice.

1
ehm, are you sure add_shortcode('toggle', 'ec_toggle_content') is supposed to be INSIDE the function AND after a return statement ? - Florian Humblot
I followed this guide mosaikweb.com/toggle-plus-shortcode, it's the first time I find myself templating a shortcode - Emi-C
@FMashiro I followed your hint to make it work, if you wanna post an answer I'll be glad to mark it as correct. Thanks. - Emi-C
It's fine, mark your own as accepted :) - Florian Humblot

1 Answers

1
votes

The following guide was misleading mosaikweb.com/toggle-plus-shortcode

Basically as hinted by FMashiro, i moved

add_shortcode('toggle', 'ec_toggle_content');

at the top of my sample code, the working result is

add_shortcode('toggle', 'ec_toggle_content');

function ec_toggle_content($atts, $content = null){

   $togtit='Title test';

   extract(shortcode_atts(array('togtit'=> ''), $atts));

   $output .= '<h4 class="toggletrg">'.$title.' <i class="fa fa-plus right toggleicon"></i><i class="fa fa-minus right toggleicon hidden"></i></h4>';
   $output .= '<div class="togglecont">';
   $output .= do_shortcode($content);
   $output .= '</div>';
   return $output;

}