Hide specific nav elements based on non-local state of php wordpress ip

For a production extranet site, I have a website that is fully accessible indoors. The website is now fully accessible from the outside as well. However, there are some Level 1 items containing submenu items that are confidential and should only be used in a production environment.

In php I know how to redirect site visitors to restricted page based on ip address. But this is a primitive solution that interrupts the user's work. I also prefer not to password-protect the pages because that would also require the user to fill in that password in production.

This is the code I used to redirect:

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

      

I was wondering how I can apply this logic to wp_nav to exclude certain menu items from navigation?

+3


source to share


3 answers


I would like to start by saying that I am not a WordPress developer first. However, from what I can tell you should be using a custom walker for your nav menu. Within your custom walker, you can conditionally add or remove navigation options you / don't want to show based on the IP code you specify. However, you still want to keep this code on restricted pages, so if the link is shared outside, they still don't have access.

A cleaner way to limit IP addresses is to use .htaccess. You have to configure it to restrict certain web pages to local access only. This combined with a regular walker would be the cleanest.

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



Some custom 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

0


source


Depending on how your theme handles the menu, you can just make 2 menus and display them based on the IP

<?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 only those pages will still be found using the direct url. If you want to prevent this, you should take a look at the power of .htaccess files.

0


source


You can use the code as it is. But instead, if the user redirection just does nothing.

The code looks something like this:

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

      

0


source







All Articles