Shortcode producing headers already sent error

The adjustment to my function below fixed the issue but I wanted to make sure that this is the right way to handle the shortcode. I’ve added ob_start(); and ob_get_clean(); to the function. Is this the correct way to handle something like this?

function site_map2( $atts ) {
    $atts = shortcode_atts(
        array(
            'exclude' => '',
        ), $atts );
        ob_start();
        echo '<ul>' . wp_list_pages('echo=false' . '&exclude=" . $atts["exclude'] . '&sort_column=post_title&title_li=') . '</ul>';
        return ob_get_clean();
}
add_shortcode( 'SiteMap2', 'site_map2' );

Leave a Comment