• Resolved Colin

    (@colinsafranek)


    I am using WP 4.8, with Themeco’s “Pro” theme.

    When I go to Appearance >> Menus the checkbox is appearing on each menu item as it’s supposed to, and all the conditional logic options are displaying properly if I check the box, but my preferences get reset when I save changes (i.e. “Update Menu” button). After saving the menu, all the checkboxes are unchecked. It’s like something is preventing the options from saving. But I see no console errors even after clicking the “Update Menu” button.

    Please help!

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter Colin

    (@colinsafranek)

    Perhaps you could recommend a troubleshooting technique to find out why the If Menu options are not being saved. Without any console errors, I don’t know where to turn…

    Try enabling debugging in WordPress to log any PHP errors that may occur when the conditional logic is saved in the database.
    https://codex.wordpress.org/Debugging_in_WordPress

    Do you have any other menu plugins installed that may be interfering?

    Are you able to test it with a default theme like TwentySeventeen just to rule out your pro theme? (Maybe put the site in maintenance mode first.)

    Thread Starter Colin

    (@colinsafranek)

    Thanks Chris. I have determined that it is the Pro theme that is interfering. It also adds options/fields to each menu item in the Admin UI. I have opened a ticket with the Pro theme support team. Can you think of any specific hooks or filters that Pro theme might be using that would be conflicting with If Menu? That may help them troubleshoot.

    Cheers,
    – Colin

    Thread Starter Colin

    (@colinsafranek)

    Here are some errors in the debug.log as well:

    PHP Notice:  Undefined index: Active Session Plan Member (any) in /wp-content/plugins/if-menu/if-menu.php on line 119
    PHP Warning:  call_user_func() expects parameter 1 to be a valid callback, no array or string given in /wp-content/plugins/if-menu/if-menu.php on line 119
    PHP Notice:  Undefined offset: 9590 in /wp-content/plugins/if-menu/if-menu.php on line 236
    PHP Warning:  Invalid argument supplied for foreach() in /wp-content/plugins/if-menu/if-menu.php on line 236
    

    FYI, the “Active Session Plan Member (any)” index is the name of one of my custom conditions I’ve created using the if_menu_conditions filter.

    Do any of those console errors give you any hints about what might be going wrong?

    Thanks.

    They point to the problem mentioned in the first FAQ.

    The bad news is that there’s not much that Themeco can do about it. The good news is that the core issue may be resolved in WordPress 4.9, according to the trac ticket.

    Themes are loaded after plugins so they get the last word. If you don’t use the menu options added by the theme, I think the only workaround is to try to disable those.

    Thread Starter Colin

    (@colinsafranek)

    Ah, I see :-/ Darn.

    Ok. Well, let’s hope for that 4.9 release 🙂

    Thanks for your help. I believe I can achieve most of the functionality I lost by adding body classes for my conditions, and using CSS to show/hide menu items based on those classes. But I really like the idea of removing menu items entirely, rather than just visually hiding them.

    Perhaps I can accomplish the same thing If Menu is doing, but without the UI? Are you using a filter to intercept and change the Menu output? It not the Menu Walker class is it? Where should I begin reading in the WP docs if I wanted to do this?

    Thanks again for your help.

    Plugin Author Andrei

    (@andreiigna)

    Hi Colin,

    Sorry for the delayed answer.

    I took a look at the issue and I may know what’s causing this. Do you have multiple translations on the website? This causes problems sometimes.

    The latest update of the plugin fixes this.
    It looks you have custom rules added by a plugin or theme. Please update the code as well, it has a new field id which is required now https://github.com/AndreiIgna/if-menu#adding-custom-rules-in-a-plugin-or-theme

    Cheers

    Thread Starter Colin

    (@colinsafranek)

    Hi Andrei, thanks for the update, but the If Menu options, even the built-in conditions like “if user is logged in,” still won’t save. They just get wiped completely. So I think there is a much larger conflict going on here with my theme (i.e. the issue mentioned in the first FAQ).

    I’ve used the `wp_get_nav_menu_items’ filter to hook into the menu output and run my own conditionals. Is this how If Menu does it? If you have a moment to look at my code I would love to hear your opinion. This was the most efficient method I could come up with, but my concern is that the nested foreach iterations might be too heavy on page load speed, since this hook runs on every new page load. But maybe there is no way around it (?). Thanks!

    function wuwo_conditional_nav_logic( $items, $menu, $args )
    {
      // write_log( $items );
    
      global $session_plan_slugs;
      $logged_in = get_current_user_id();
    
      // first make sure we are NOT in the backend (admin):
      if( ! is_admin() )
      {
        // Iterate over the items to search and destroy by class:
        foreach( $items as $key => $item )
        {
          $classes = $item->classes;
    
          // Hide 'customer' menu items from logged-out visitors:
          if( ! $logged_in && in_array( 'customer', $classes ) )
          {
            unset( $items[$key] );
          }
    
          // First check if user is logged in:
          if( $logged_in )
          {
            $user_id = $logged_in;
    
            // Hide 'visitor' menu items from logged in users:
            if( in_array( 'visitor', $classes ) )
            {
              unset( $items[$key] );
            }
    
            // Hide "The Goods" submenu plan links from non-active members of those plans:
            foreach( $session_plan_slugs as $plan )
            {
              // if current menu item has current plan class,
              // and the current user IS NOT an active member of that plan
              // then kill current menu item:
              if( in_array( $plan, $classes ) &&  ! wc_memberships_is_user_active_member( $user_id, $plan ) )
              {
                unset( $items[$key] );
              }
            }
          }
    
        }
      }
    
      return $items;
    }
    
    add_filter( 'wp_get_nav_menu_items', 'wuwo_conditional_nav_logic', null, 3 );
    Plugin Author Andrei

    (@andreiigna)

    If the code works, I would say it’s ok 😀

    One thing which can be improved is the caching of wc_memberships_is_user_active_member result. If there are 5 menu items and 5 plans there are 25 calls to that function which slows down a bit the filtering.
    It should call that function just once per plan, and store the result in $session_plan_slugs or a new variable

    I’ll check again later and maybe try to change the new code.

    Thread Starter Colin

    (@colinsafranek)

    Hey thanks Andrei! That’s a great suggestion.

    What I ended up doing is adding menu items, rather than deleting them. This significantly cuts down on those wc_memberships_is_user_active_member calls. Now I iterate through the $session_plan_slugs array only once to determine which plans the current user is an active member of, and then insert those membership links as sub-menu items, so I only have to call the membership test function once per plan.

    The other major benefit of this approach is that I no longer have to manually maintain the membership menu links in the WP Admin interface. Now I just have the top-level menu item, and its children sub-menu items get added dynamically, so if I add new membership plans in the future they automatically get inserted into the front-end menu based on the current logged-in user’s active plans. Huge improvement!

    Thanks a lot for your help.

    Cheers!
    – Colin

    • This reply was modified 8 years, 8 months ago by Colin.
Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Conditional Logic Options Not Saving’ is closed to new replies.