title: How to add a submenu dynamically in Wordpress PHP?
body: <p>I'm trying to add a login/logout feature on my new website at dernier.com. How do I add login/logout as a submenu under My Account?</p>
  
  <p>I've looked up every document for the Wordpress menu, tried looking up solutions. But nothing so far. </p>
  
  <pre><code>function add_loginout_link( $items, $args ) {
      $parent_title = get_the_title($post-&gt;post_parent);
      //echo $parent_title;
      //var_dump($items); 
  
      $menu_items = wp_get_nav_menu_items("Main Menu 2");
      foreach ($menu_items as $value) {
          //echo $value-&gt;title;  
          //echo $value-&gt;slug;
          if($value-&gt;title == "My Account"){
              //echo var_dump($value);
              $x = get_post( wc_get_page_id( 'myaccount' ) ); 
              $x-&gt;menu_item_parent = "1377";
              $test = new WP_POST($x);
              //echo var_dump($test);
              $items[] = $test; 
  
          }
      }
    return $items;
  
  }
  
  &gt; I get the following error:  Your PHP code changes were rolled back due
  &gt; to an error on line 443 of file wp-content/themes/rosa/functions.php.
  &gt; Please fix and try saving again.   Uncaught Error: [] operator not
  &gt; supported for strings in wp-content/themes/rosa/functions.php:443 
  &gt; Stack trace:
  &gt; #0 wp-includes/class-wp-hook.php(286): add_loginout_link('&lt;li id="menu-it...', Object(stdClass))
  &gt; #1 wp-includes/plugin.php(208): WP_Hook-&gt;apply_filters('&lt;li id="menu-it...', Array)
  &gt; #2 wp-includes/nav-menu-template.php(243): apply_filters('wp_nav_menu_ite...', '&lt;li id="menu-it...',
  &gt; Object(stdClass))
  &gt; #3 wp-content/themes/rosa/header.php(138): wp_nav_menu(Object(stdClass))
  &gt; #4 wp-includes/template.php(704): require_once('/var/www/UshaFo...')
  &gt; #5 wp-includes/template.php(653): load_template('/var/www/UshaFo...', true)enter code here
  &gt; #6 wp-includes/general-template.php(41): locate_template(Array, true)
  &gt; #7 wp-content/themes/rosa/page.php(9): get_header()
  &gt; #8 wp-includes/template-loader.php(77): include('/var/www/UshaFo...')
  &gt; #9 /var/www/U
  </code></pre>