-1

I am trying to add a new menu item to an already existing nav menu in my theme, I am using some part of this answer How to Hard Code Custom menu items

I am directly using this code in my plugin

wp_update_nav_menu_item(2, 0, array(
    'menu-item-title' =>  __('Home'),
    'menu-item-classes' => 'home',
    'menu-item-url' => home_url( '/' ), 
    'menu-item-status' => 'publish'));

But it gives me following error

Your PHP code changes were rolled back due to an error on line 357 of file C:\xampp\htdocs\wordpress\wp-includes\link-template.php. Please fix and try saving again

Call to a member function get_page_permastruct() on null

  • never edit you code from the wordpress "code editor" – Mark Kaplun Apr 2 '18 at 10:40
  • @MarkKaplun Why ? , and where should i edit it then – beginner Apr 2 '18 at 10:45
  • Why? because it is extremely insecure practice, and it is easy to make an edit that will break your site without an ability to remove it. Use a proper editor, with an integrated FTP support, or an FTP software that can detect local changes and upload to the server "automagically" – Mark Kaplun Apr 2 '18 at 12:32
0

adding it to 'init' action hook works

function update_menu_custom(){
wp_update_nav_menu_item(2, 0, array(
    'menu-item-title' =>  __('Home'),
    'menu-item-classes' => 'home',
    'menu-item-url' => home_url( '/' ), 
    'menu-item-status' => 'publish'));
}

add_action('init','update_menu_custom');

But it still doesn't works as expected, It adds too many menu items with name home, But that is a different issue

  • register_activation_hook – Gavin Simpson Jul 23 '18 at 11:03

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.