Support » Plugins » Hacks » Main Menu -> Dynamic Select Box -> Need Ideas

  • Resolved rdcravens

    (@rdcravens)


    OK, so I have what seems to be a unique issue until I ask it and then find the list of answers I could not find for a week 🙂

    I have a select box on a page, lets call it the fruit selection box. This box updates the page (posts to self) to change the content based on the fruit selected. It also writes to the DB so that the site can remember what fruit you last looked at the next time you visit (so it passes variables – not just a page changer).

    The list of fruits is created by the user and stored. The user can store up to 20 different fruits and each fruit possibility has its own column in the users table (for a reason) – so 20 potential columns with data coming in and one select box.

    $change_list = htmlentities($_REQUEST['which_list']);
    
    if ($change_list)
    {
    	$update = $wpdb->update('wp_users', array('active_list' => $change_list), array('ID' => $user_id), array('%f'));
    	if ( is_wp_error( $update ) ) { echo 'Error Updating Active List'; }
    }
    
    if ($change_list)
    {
    	$which_list = $change_list;
    } else {
    	$which_list = $current_user->active_list;
    }
    
    // ------------ Create our drop down list selector
                          echo '<td style="width:80%; text-align:left;"><form name="whichlist" action="" method="POST"><select style="cursor:pointer; border:0px; outline:0px;" name="which_list" onChange="document.whichlist.submit()">';
    $i = 1;
    while ($i <= 20)
    {
      $sel = '';
      $list_name = $current_user->{'fruit' . $i . '_name'};
      if ($which_list == $i){ $sel = 'selected'; }
      if ($list_name) { echo '<option value="' . $i . '" '. $sel .'>' . $list_name . '</option>'; }
      $i++;
    }
    echo '</select></form></td>';
    // ------------ End Selector

    All is fine and dandy to this point (works like a champ)… Now the tricky part (the part I can not solve)…

    I would like the dynamically generated select list to be added to the navigation menu as a drop down select box like it would be seen on the page it is on now (drop down arrow, shows selected item on the list)…

    I do not want to dynamically create a whole menu system, just add one top level menu item to the existing menu when the page opens (every time). I prefer to replace a menu placeholder so that I can move it around the menu as needed using the menu interface.

    Is this possible? I am using the standard WP Menu system.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Yes it is! You would first create a placeholder menu item and insert it in your menu. The Link type will be fine for this.

    Then you just substitute the link HTML with the actual dropdown HTML as determined by your script. The easiest way is to use the ‘wp_nav_menu’ filter and simply search and replace the placeholder HTML with the dropdown using string functions. A more elegant solution would be to create or extend a nav_menu walker object that inserts the dynamic dropdown code when the appropriate placeholder menu object is encountered, otherwise it works as usual.

    You can specify your new walker either on your theme’s call to wp_nav_menu() or by using the ‘wp_nav_menu_args’ filter.

    So great feedback, I was able to wrap my head around what you were saying and get the drop down menu created and dynamically pulling the info as expected…

    I really appreciate the help!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Main Menu -> Dynamic Select Box -> Need Ideas’ is closed to new replies.