{"id":120,"date":"2016-07-15T22:07:44","date_gmt":"2016-07-15T22:07:44","guid":{"rendered":"https:\/\/alexrusin.com\/?p=120"},"modified":"2019-04-15T23:44:03","modified_gmt":"2019-04-15T23:44:03","slug":"fixing-menu-issue-on-wordpress-single-page-themes","status":"publish","type":"post","link":"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/","title":{"rendered":"Fixing Menu Issue on WordPress Single-Page Themes"},"content":{"rendered":"

Among WordPress themes out there, there are so called single- or one-page themes.  Most of the content of the page is located on one page.  Menu links are route to different sections of the page via anchors tags.  The menu is created using custom anchor links like so:<\/p>\n

\"Untitled-1\"<\/p>\n

The corresponding  part of an anchor tag looks like this <a name=”#resume”><\/a><\/strong><\/p>\n

This anchor link only works if you are jumping to a specific spot on the same page.  For instance, if you have a blog page different from your home page unless you put the full URL of the home page like so http:\/\/www.mywebsite.com#resume.  <\/strong>So, not a big deal, let’s replace Custom Link URL on the menu page with the full URL of the home page. Right?  Well, this solution works but it makes all the menu links on home page to be highlighted, because each of them “think” they are on the correct page.  It does not look good.<\/p>\n

My solution to the problem is to dynamically prefix an anchor link with “http:\/\/www.mywebsite.com” when it is not on the homepage.  The piece of code below does exactly that.<\/p>\n

\n       add_filter( 'wp_nav_menu_objects', 'em_links');\n\tfunction em_links ( $sorted_menu_items, $args ) {\n\t\tif(!is_front_page()){\t \n  \t\tforeach ($sorted_menu_items as $menu_item) {\n\t\n\t\t\t$menuLink = $menu_item->url;\n\t\t\tif(!preg_match(\"~^(?:f|ht)tps?:\/\/~i\", $menuLink)){\n   \t\t\t\t$menu_item->url = get_bloginfo('url') . $menuLink;\n\t\t\t}\n\n   \t\t}\n     \n \t\t}\n\treturn $sorted_menu_items;\n\t}\n<\/code>\n<\/pre>\n

We change nav_menu <\/strong>code by hooking into wp_nav_menu_objects<\/strong> filter. $sorted_menu_items <\/strong>and $args <\/strong>are passed from the filter.  $args <\/strong>are arguments created by the user, but we not gonna touch them.  We will work with $sorted_menu_items.  <\/b>It is an array of objects.  First we make sure, we are not<\/strong> on front page.  Then we start looping through all the objects and checking if link starts with a ‘#’ sign.  Actually, in the code above I am checking if the link does not start with ‘http’ or https’ because I felt like playing with regular expressions.  For a simple check if a string starts with ‘#’ character substr<\/span>(<\/span> $menuLink<\/span>,<\/span> 0<\/span>,1<\/span> )<\/span> ===<\/span> “#”<\/span><\/strong> should do.  If the link starts with ‘#’ character, we need to append website’s url<\/strong> to it.  In the end, do not forget to return $sorted_menu_items<\/strong>.  This is the difference of filter hook from action hook.  In filter hook you are passed an argument.  You do something to it and then return it.<\/p>\n

 <\/p>\n

Download code plugin<\/strong><\/a><\/p>\n

 <\/p>\n","protected":false},"excerpt":{"rendered":"

Among WordPress themes out there, there are so called single- or one-page themes.  Most of the content of the page is located on one page.  Menu links are route to different sections of the page via anchors tags.  The menu is created using custom anchor links like so: The corresponding  part of an anchor tag…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-120","post","type-post","status-publish","format-standard","hentry","category-wordpress-development"],"yoast_head":"\nFixing Menu Issue on WordPress Single-Page Themes | Alex Rusin Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Fixing Menu Issue on WordPress Single-Page Themes | Alex Rusin Blog\" \/>\n<meta property=\"og:description\" content=\"Among WordPress themes out there, there are so called single- or one-page themes.  Most of the content of the page is located on one page.  Menu links are route to different sections of the page via anchors tags.  The menu is created using custom anchor links like so: The corresponding  part of an anchor tag...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/\" \/>\n<meta property=\"og:site_name\" content=\"Alex Rusin Blog\" \/>\n<meta property=\"article:published_time\" content=\"2016-07-15T22:07:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-04-15T23:44:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blog.alexrusin.com\/wp-content\/uploads\/2016\/07\/Untitled-1.jpg\" \/>\n<meta name=\"author\" content=\"alexrusin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"alexrusin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/\"},\"author\":{\"name\":\"alexrusin\",\"@id\":\"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a\"},\"headline\":\"Fixing Menu Issue on WordPress Single-Page Themes\",\"datePublished\":\"2016-07-15T22:07:44+00:00\",\"dateModified\":\"2019-04-15T23:44:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/\"},\"wordCount\":414,\"publisher\":{\"@id\":\"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a\"},\"image\":{\"@id\":\"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/blog.alexrusin.com\/wp-content\/uploads\/2016\/07\/Untitled-1.jpg\",\"articleSection\":[\"WordPress Development\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/\",\"url\":\"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/\",\"name\":\"Fixing Menu Issue on WordPress Single-Page Themes | Alex Rusin Blog\",\"isPartOf\":{\"@id\":\"https:\/\/blog.alexrusin.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/blog.alexrusin.com\/wp-content\/uploads\/2016\/07\/Untitled-1.jpg\",\"datePublished\":\"2016-07-15T22:07:44+00:00\",\"dateModified\":\"2019-04-15T23:44:03+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/#primaryimage\",\"url\":\"https:\/\/blog.alexrusin.com\/wp-content\/uploads\/2016\/07\/Untitled-1.jpg\",\"contentUrl\":\"https:\/\/blog.alexrusin.com\/wp-content\/uploads\/2016\/07\/Untitled-1.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/blog.alexrusin.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Fixing Menu Issue on WordPress Single-Page Themes\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.alexrusin.com\/#website\",\"url\":\"https:\/\/blog.alexrusin.com\/\",\"name\":\"Alex Rusin\",\"description\":\"Web Development Blog\",\"publisher\":{\"@id\":\"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.alexrusin.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a\",\"name\":\"alexrusin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blog.alexrusin.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c36ef231f9e0b11371891eb84360f4bc?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c36ef231f9e0b11371891eb84360f4bc?s=96&d=mm&r=g\",\"caption\":\"alexrusin\"},\"logo\":{\"@id\":\"https:\/\/blog.alexrusin.com\/#\/schema\/person\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Fixing Menu Issue on WordPress Single-Page Themes | Alex Rusin Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/","og_locale":"en_US","og_type":"article","og_title":"Fixing Menu Issue on WordPress Single-Page Themes | Alex Rusin Blog","og_description":"Among WordPress themes out there, there are so called single- or one-page themes.  Most of the content of the page is located on one page.  Menu links are route to different sections of the page via anchors tags.  The menu is created using custom anchor links like so: The corresponding  part of an anchor tag...","og_url":"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/","og_site_name":"Alex Rusin Blog","article_published_time":"2016-07-15T22:07:44+00:00","article_modified_time":"2019-04-15T23:44:03+00:00","og_image":[{"url":"https:\/\/blog.alexrusin.com\/wp-content\/uploads\/2016\/07\/Untitled-1.jpg","type":"","width":"","height":""}],"author":"alexrusin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"alexrusin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/#article","isPartOf":{"@id":"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/"},"author":{"name":"alexrusin","@id":"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a"},"headline":"Fixing Menu Issue on WordPress Single-Page Themes","datePublished":"2016-07-15T22:07:44+00:00","dateModified":"2019-04-15T23:44:03+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/"},"wordCount":414,"publisher":{"@id":"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a"},"image":{"@id":"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/#primaryimage"},"thumbnailUrl":"https:\/\/blog.alexrusin.com\/wp-content\/uploads\/2016\/07\/Untitled-1.jpg","articleSection":["WordPress Development"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/","url":"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/","name":"Fixing Menu Issue on WordPress Single-Page Themes | Alex Rusin Blog","isPartOf":{"@id":"https:\/\/blog.alexrusin.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/#primaryimage"},"image":{"@id":"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/#primaryimage"},"thumbnailUrl":"https:\/\/blog.alexrusin.com\/wp-content\/uploads\/2016\/07\/Untitled-1.jpg","datePublished":"2016-07-15T22:07:44+00:00","dateModified":"2019-04-15T23:44:03+00:00","breadcrumb":{"@id":"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/#primaryimage","url":"https:\/\/blog.alexrusin.com\/wp-content\/uploads\/2016\/07\/Untitled-1.jpg","contentUrl":"https:\/\/blog.alexrusin.com\/wp-content\/uploads\/2016\/07\/Untitled-1.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.alexrusin.com\/"},{"@type":"ListItem","position":2,"name":"Fixing Menu Issue on WordPress Single-Page Themes"}]},{"@type":"WebSite","@id":"https:\/\/blog.alexrusin.com\/#website","url":"https:\/\/blog.alexrusin.com\/","name":"Alex Rusin","description":"Web Development Blog","publisher":{"@id":"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.alexrusin.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a","name":"alexrusin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.alexrusin.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c36ef231f9e0b11371891eb84360f4bc?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c36ef231f9e0b11371891eb84360f4bc?s=96&d=mm&r=g","caption":"alexrusin"},"logo":{"@id":"https:\/\/blog.alexrusin.com\/#\/schema\/person\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/posts\/120","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/comments?post=120"}],"version-history":[{"count":10,"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/posts\/120\/revisions"}],"predecessor-version":[{"id":5877,"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/posts\/120\/revisions\/5877"}],"wp:attachment":[{"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/media?parent=120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/categories?post=120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/tags?post=120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}