I’ve created a shortcode for a sitemap page. When I make a change on the page and click update I get a headers already sent error. Below is my shortcode and the two error messages. I’m wondering if I need to work ob_start()
and ob_get_clean()
into my function but I’m not 100% positive how it should be formatted.
function site_map2( $atts ) {
$atts = shortcode_atts(
array(
'exclude' => '',
), $atts );
return '<ul>' . wp_list_pages('exclude=" . $atts["exclude'] . '&sort_column=post_title&title_li=') . '</ul>';
}
add_shortcode( 'SiteMap2', 'site_map2' );
Here are the error messages:
Warning: Cannot modify header information – headers already sent by (output started at /public_html/wp-includes/post-template.php:1205) in /public_html/wp-admin/post.php on line 197
Warning: Cannot modify header information – headers already sent by (output started at /public_html/wp-includes/post-template.php:1205) in /public_html/wp-includes/pluggable.php on line 1167
2 Answers
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' );