• Resolved neethab

    (@neethab)


    Hi,

    I have been searching on the internet for an example of visibility condition which is not in-built WordPress condition( like is_home()). From what I have come across, I am not able to figure out how to do what I need.

    I want to show menu items which are present in an array in a custom made plugin.
    For instance, I have an array $test_var = array(“welcome”, “Search”, “Display”); Now I want to give a visibility condition on each menu item to check if its name( or corresponding string ) is present in this array. If yes, the menu item should be displayed otherwise should not be displayed.

    Can you please help with this?

    Thanks,
    Neetha

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author shazdeh

    (@shazdeh)

    In the Visibility field, you can access $item variable which is the WP_Post object containing data about the current menu item being processed. So you could easily do:

    
    in_array( $item->post_title, array( 'welcome', 'Search', 'Display' ) )
    
    Thread Starter neethab

    (@neethab)

    Hi Shazdeh,

    Thanks for the reply.

    Visibility works properly when I give the code you have given. The problem is, I cannot give the whole array like that. I have to use the array variable($test_var) as the array will be dynamically changing according to the user license for each user. I tried using “in_array ( “welcome” , $test_var)”. Unfortunately its not working. $test_var is a variable in a php page. I am able to print its value in the website but not in admin area.

    How can I fix this?

    Thanks,
    Neetha

    Thread Starter neethab

    (@neethab)

    Hi Shazdeh,

    The problem is solved.

    It was not working as the array was not getting passed to the admin area of WordPress. When I assigned the array to a session it started working.
    Gave the following code on the php file, $_SESSION[‘feature_array’]=$test_var; Then the visibility condition was given as in_array ( “Welcome!” , $_SESSION[‘feature_array’]).

    Thank-you so much for the help and this great plugin.

    Regards,
    Neetha

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Check if the menu name is present in an array and show/hide accordingly’ is closed to new replies.