Support » Plugin: Menu Item Visibility Control » Restrict menu visibility by IP addresses

  • Resolved dkjumper

    (@dkjumper)


    Hi all,
    I’ve tried to limit the menues to only show when following conditions are met:
    (is_user_logged_in() OR ($_SERVER[‘REMOTE_ADDR’]==”x.x.x.x”))

    That works fine, but I want to define an array in e.g. my wp-config.php where I could whitelist all the IP’s in order to make the menu visible condition less complex…

    wp-config.php contains following:
    $Approved_IP_List = array(“x.x.x.x”,”y.y.y.y”,”z.z.z.z”);
    if (in_array($_SERVER[‘REMOTE_ADDR’], $Approved_IP_List))
    $Approved_IP = True;
    else
    $Approved_IP = False;

    I’ve tried setting the “global $Approved_IP;” but it didn’t help. I keep getting this same error over and over again.
    Undefined variable: Approved_IP in /path_to_wp/wp-content/plugins/menu-items-visibility-control/init.php(113) : eval()’d code on line 1

    If I try using the $Approved_IP in some of my webpages, it works flawlessly – it’s only creating trouble in the “Menu Item Visibility Control” plugin

    Hope you can help me solve this 😉

    Thanks in advance
    Johnny

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

    (@shazdeh)

    Hi!

    Going by the snippet you have provided, in the Logic field you can put:

    
    $GLOBALS['Approved_IP']
    

    Now weather $Approved_IP is true or false, it will determine how the menu item will be displayed.
    I’d recommend to encapsulate this in a function, like:

    
    function my_is_approved_ip() {
       // check ip here and return true or false.
    }
    

    Then in the menu item you can do:

    
    my_is_approved_ip()
    

    Which is much cleaner and doesn’t pollute the global scope.

    https://gardeninghintsandtipsuk.com/wp-content/uploads/2018/11/Screen-Shot-2018-11-11-at-16.42.04.png

    I’m not quite sure where you mean for me to put these codes. I’ve put…

    function my_is_approved_ip() {
    // check ip here and return true or false.
    }

    into the code of Theme Functions (functions.php)

    But not sure where to put…
    my_is_approved_ip()

    Thanks

    Hi again

    @shazdeh
    thank you so much for a quick response… it works perfectly with a function both in the menu and in my pages as well – I can’t believe why I didn’t thought of functions myself – but anyway a big applause for your help 😉

    @singinggardener
    You need to find your menu administration tab, then go to “menu structure” and expand the menu item you want to “limit”…:
    When you have expanded the item, you should have “Label” and “Title” in the top, and just below you should find a field named “Visibility” – this is the place to put “my_is_approved_ip()” without the quotes.

    Best regards
    Johnny

    sorry i’m in the wrong question

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Restrict menu visibility by IP addresses’ is closed to new replies.