3

For a work extranet website i have a website that's fully accessible on premises. The website is now also fully accessible from outside as well. There are however some level 1 nav items with sub menu items that are confidential, and should only be used within the working environment.

In php i know how to redirect website visitors to a restricted page, based on there ip address. But this is a primitive solution, that interrupts the user experience. I also rather not password protect the pages, because this would also require the user to fill in that password within the working environment.

This is the code that i used for redirecting:

<?php
if (!preg_match('/^192/', $_SERVER['REMOTE_ADDR'])) {
header('HTTP/1.1 403 Forbidden');
header('Location: notallowed.php');
exit;
}
?>

I was wondering, how can i apply this logic to the wp_nav to exclude specific menu items from the navigation?

user3191748
  • 33
  • 1
  • 4

3 Answers3

0

I'd like to first start by saying I'm not a WordPress developer. That said, from what I can tell you should be able to use a custom walker for your nav menu. Within your custom walker, you could conditionally add or remove the nav options you do/don't want to show based on the IP code you have listed above. You'd still want to keep that code in restricted pages, however, so if a link is shared externally they still don't have access.

A cleaner way to restrict IP addresses is to use .htaccess. You should be able to configure it to restrict certain webpages to local access only. This coupled with the custom walker would be cleanest.

http://premium.wpmudev.org/blog/limit-access-login-page/

Some custom walker tutorials:

https://wordpress.stackexchange.com/questions/14037/menu-items-description-custom-walker-for-wp-nav-menu/14039#14039

http://code.tutsplus.com/tutorials/understanding-the-walker-class--wp-25401

Community
  • 1
  • 1
Grant Amos
  • 2,256
  • 17
  • 11
0

Depending on how your theme handles the menu, you could simply make 2 menu's and display them based on ip address

<?php if (!preg_match('/^192/', $_SERVER['REMOTE_ADDR'])): ?>
<?php wp_nav_menu('menu=full'); ?>
<?php else: ?>
<?php wp_nav_menu('menu=simple'); ?>
<?php endif; ?>

Also with only this the pages would still be found using a direct URL. If you want to prevent this you should have a look at the power of .htaccess files.

Rein Baarsma
  • 1,466
  • 13
  • 22
0

You can use the code the way you have it.. But instead if redirecting the user just have it do nothing..

The code would look something like this:

<?php
if (!preg_match('/^192/', $_SERVER['REMOTE_ADDR'])) {
   # Do Nothing 
}else{
   # Show Menu Item
}
?>
Yaco Zaragoza
  • 429
  • 1
  • 7
  • 18