0

I am trying to insert a simple 1 field contact 7 form into my menu.

I have this code working for just text, but if I try to insert the shortcode for the form, I get an error. What am I doing wrong?

add_filter( 'wp_nav_menu_items', 'your_custom_menu_item', 8, 2 );
function your_custom_menu_item ( $items, $args ) {
if( $args->theme_location == 'primary')  {
    $items .= '<li class="menu-item">Text goes here</li>';
}
return $items;
}

Here I try to insert shortcode for form:

add_filter( 'wp_nav_menu_items', 'your_custom_menu_item', 8, 2 );
function your_custom_menu_item ( $items, $args ) {
if( $args->theme_location == 'primary')  {
    $items .= '<li class="menu-item">echo do_shortcode( '[contact-form-7 id="1323" title="Newsletter"]' );</li>';
}
return $items;
}
0

Well, you're getting error, because your code is not correct... There is syntax error in this line (You combine string and PHP code in there...):

$items .= '<li class="menu-item">echo do_shortcode( '[contact-form-7 id="1323" title="Newsletter"]' );</li>';

It should look like this:

add_filter( 'wp_nav_menu_items', 'your_custom_menu_item', 8, 2 );
function your_custom_menu_item ( $items, $args ) {
    if( $args->theme_location == 'primary')  {
        $items .= '<li class="menu-item">'. do_shortcode( '[contact-form-7 id="1323" title="Newsletter"]' ) .'</li>';
    }
    return $items;
}
  • Thanks a lot, I am not very comfortable with my PHP. – NFdesign Jun 3 '15 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.