Error syntax error, unexpected T_CONSTANT_ENCAPSED_STRING [closed]

1
add_filter('wp_nav_menu_items', 'add_search_form', 10, 2);
function add_search_form($items, $args) {
    if( $args->theme_location == 'primary' )
        $items .= '<li><div class="sp-search"><div class="top-search"><?php echo do_shortcode('[yith_woocommerce_ajax_search]'); ?></div></div></li>';
        return $items;
}

How can I insert this shortcode " <?php echo do_shortcode('[yith_woocommerce_ajax_search]'); ?> " into div <div class="top-search"> ... </div> correctly?

Show this error:

Parse error: syntax error, unexpected ''); ?></div></div></li>'' (T_CONSTANT_ENCAPSED_STRING) in functions.php on line 137 
    
asked by anonymous 09.07.2016 / 02:09

1 answer

4

The error is in this line:

$items .= '<li><div class="sp-search"><div class="top-search"><?php echo do_shortcode('[yith_woocommerce_ajax_search]'); ?></div></div></li>';

You can not put PHP inside string and nor echo , the correct would be to concatenate the string, like this:

if( $args->theme_location == 'primary' ) {
    $items .= '<li><div class="sp-search"><div class="top-search">' .
              do_shortcode('[yith_woocommerce_ajax_search]') .
             '</div></div></li>';

    return $items;
}
    
09.07.2016 / 02:21