Wordpress font displaying above widget view

I added the following filter to my functions.php file:

add_filter('widget_text', 'do_shortcode'); 

      

This allowed shortcode in my widgets as expected.

However, the shortcode content is now displayed above the title. What am I doing wrong?

This is what I am repeating through 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;

      

+3


source to share


2 answers


The problem is what you are calling echo()

in your shortcode, not in return

. If you count the ordering the functions call, then it apply_filters( 'widget_text' )

runs before the actual HTML widget is output, so yours echo

as part of your filter makes it print too early. To correct this return

instead of the echo

results.



add_filter('widget_text', 'do_shortcode'); 
add_shortcode('events', 'events_shortcode' );
function events_shortcode( $atts, $content=null ){
    // set default attribute values and extract to variables
    extract( shortcode_atts( array( 'attribute' => 'default_value' ), $atts ) );

    // populate your variables

    $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>';

    // return, don't echo
    return $events_msg;
}

      

+3


source


When you added a filter to your .php functions adding the following may have also solved the problem



add_filter( 'widget_text', 'shortcode_unautop' );

      

0


source







All Articles