Error syntax error, Unexpected T_CONSTANT_ENCAPSED_STRING

Asked

Viewed 866 times

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]'); ?>" inside the div <div class="top-search"> ... </div> correctly?

Shows this error:

Parse error: syntax error, unexpected ''); ?></div></div></li>'' (T_CONSTANT_ENCAPSED_STRING) in functions.php on line 137 
  • And what error appears? Syntax error there are several, if can inform will facilitate ;)

  • Parse error: syntax error, Unexpected ''); ? ></div></div></li>'' (T_CONSTANT_ENCAPSED_STRING) in ..... /functions.php on line 137

1 answer

4


The mistake is on this line:

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

You cannot put PHP inside a string and neither echo, the correct would be to concatenate the string, thus:

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;
}
  • had tried to concatenate without php.. but had forgotten to put the simple quotes correctly. Thanks for the help!

  • 1

    @Fabriciocorrêa if you have decided to mark the answer as correct, if you do not know how to do read this link http://meta.pt.stackoverflow.com/a/1079/3635, thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.