Why am I getting sprintf(): Too few arguments here php error here while trying to use this in items_wrap on wp_nav_menu()?
$menu = wp_get_nav_menu_object('primary-menu');
$navId = $menu->term_id;
$menuId = 'menu_' . $navId;
$navUrl = get_field('social_media_url', $menuId);
$navImg = get_field('social_media_img', $menuId);
if ($navUrl && $navImg) {
$socialOption = '<ul id="%s" class="%s">%s<li class="menu-social-option"><a href="' . esc_attr($navUrl) . '" target="_blank"><img src="' . esc_attr($navImg['url']) . '"></a></li></ul>';
} else {
$socialOption = '<ul id="%1$s" class="%2$s">%3$s</ul>';
}
$navArray = array(
'theme_location' => 'primary', // Defined when registering the menu
'menu_id' => 'primary-menu',
'container' => false,
'depth' => 2,
'items_wrap' => $socialOption,
'menu_class' => 'navbar-nav ml-auto',
'walker' => new Bootstrap_NavWalker(), // This controls the display of the Bootstrap Navbar
'fallback_cb' => 'Bootstrap_NavWalker::fallback', // For menu fallback
);
wp_nav_menu($navArray);
When I remove esc_url($navUrl) then I get this type of error uncaught ValueError: Unknown format specifier "C" in. This only happens with PHP versions 7.4+
Cheers
My solution to this issue. Turns out I don't have to use sprintf again. To wrap an extra item to wp_nav_menu with ACF variables I use this function. If any of ACF variables are empty then it just returns $items which is set by Wordpress.
function add_last_nav_item($items)
{
$menu = wp_get_nav_menu_object('primary-menu');
$navId = $menu->term_id;
$menuId = 'menu_' . $navId;
$navUrl = get_field('social_media_url', $menuId);
$navImg = get_field('social_media_img', $menuId);
if ($navUrl && $navImg) {
return $items .= '<li class="menu-social-option"><a href="' . esc_attr($navUrl) . '" target="_blank"><img src="' . esc_attr($navImg['url']) . '"></a></li>';
} else {
return $items;
}
}
add_filter('wp_nav_menu_items', 'add_last_nav_item');
$socialOption = '<ul id="%1$s" class="%2$s">%3$s</ul>';
Thank you @RiggsFolly for helping me figure this out
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Bootstrap is not exclusively a CSS framework, but its most popular features are CSS-centric. These include a powerful grid, icons, buttons, map components, navigation bars, and more.
https://getbootstrap.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.