I added the following filter in my functions.php file:
add_filter('widget_text', 'do_shortcode');
This has enabled shortcode in my widgets as expected.
However, the content of the shortcode is now displaying above the title. What am I doing wrong?
This is what I am echoing out via the shortcode:
$events_msg = '<div>You have attended <br> <span class="big-num">'. $num_actual_events . '</span> / ' . $num_total_events . ' ' . $event_type . '</br>('. $num_required_events.' required)<br><br></div>';
echo $events_msg;
The issue is that you are calling
echo()
in your shortcode rather thanreturn
‘ing the results. If you consider the order that the functions are being called, theapply_filters( 'widget_text' )
is being run before the actual widget HTML is outputted, thus yourecho
as part of your filter causes it to print out too soon. To correct thisreturn
instead ofecho
the results.When you added the filter to your functions.php, adding the following might have also solved the problem