Support » Developing with WordPress » Trying to fit a widget area output into content using add_filter

  • Resolved David Borrink


    I’m trying to add a widget area to appear at the end of the content area on single posts, but inside the post. I’ve set up a function to filter the content and add a widget area.

    I’m getting errors on the variable line where I’m declaring the widget group as a variable. Apparently my <div class="in-content"> cannot start a variable because I’m getting Parse error: syntax error, unexpected '<' in ...

    I’m trying to find out how I can get this widget code declared properly so that it can be a variable and get filtered in. Where would I find info on the correct way to syntax this? I’ve looked at other code examples for some hints, and even tried the PHP area of W3Schools, but I’m not seeing it.

    Could someone point me to a correct place to find this info?


    // Register ad spot for inside lower content area
        register_sidebar( array(
                'name' => __( 'In-Content Space', 'tto' ),
                'id' => 'in-content-ad-space',
                'description' => __( 'IN-CONTENT AD SPACE. Puts content above the Zemanta area.', 'tto' ),
                'before_widget' => '<aside id="%1$s" class="widget %2$s">',
                'after_widget' => '</aside>',
                'before_title' => '<h3 class="widget-title">',
                'after_title' => '</h3>',
        ) );
    add_filter( 'the_content', 'sb_in_content_widget' );
    function sb_in_content_widget () {
               if ( is_singular('post')) {
            $sbadspace = <div class="in-content">
                     			<?php if ( is_active_sidebar( 'in-content-ad-space' ) ) : ?>
                           		<?php dynamic_sidebar( 'in-content-ad-space' ); ?>
                     			<?php endif; ?>
               			</div><!-- .in-content-->
           $content = $content . $sbadspace;
        return $content;

    Note: I had the line $sbadspace = <div class="in-content"> set originally as $sbadspace = ?><div class="in-content"> with a ?> to keep the PHP syntax going, but the errors said it was unexpected. So my code above has a missing ?> because that’s where I’m at.

    • This topic was modified 3 years, 9 months ago by David Borrink.
Viewing 3 replies - 1 through 3 (of 3 total)
  • @davidborrink,

    You were close. The below code should get your work done.

    add_filter( 'the_content', 'sb_in_content_widget' );
    function sb_in_content_widget ( $content ) {
        if ( is_singular( 'post' ) ) {
            ob_start(); ?>
            <div class="in-content">
                <?php if (is_active_sidebar('in-content-ad-space')) : ?>
                    <?php dynamic_sidebar('in-content-ad-space'); ?>
                <?php endif; ?>
            </div><!-- .in-content-->
            $sbadspace = ob_get_clean();
            $content = $content . $sbadspace;
        return $content;


    Stack Overflow

    Maria, I thank you very much for showing me that. It works!

    Using the output buffer is a new concept for me and obviously that was what I needed to know. Somehow I knew that I was dealing with a set of content that needed to “together” and contained.

    This thought from the Stack Overflow page is the best way to describe this: “Using ob_start allows you to keep the content in a server-side buffer until you are ready to display it.” A server side buffer. It holds the info to the side and deals with it until it’s need.

    Thank you for taking the time to share this with me.

    • This reply was modified 3 years, 9 months ago by David Borrink.

    Hello David,

    Glad I was able to help.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Trying to fit a widget area output into content using add_filter’ is closed to new replies.