How to Restrict WordPress Content & Files Access by IP Addresses

PDA Access Restriction extension for Prevent Direct Access (PDA) Gold and Protect WordPress Pages & Posts (PPP) Gold plugin comes with 4 IP restriction key features:

How to Configure IP Addresses to Access Protected Content

While PPP Gold helps you prevent access to protected content based on user roles, PDA Access Restriction extension takes a step further and whitelist or blocks any IP addresses. It comes in useful when you want to restrict your protected content for internal access.

You can configure the whitelist on global as well as individual page level.

Requirements:

Whitelist IP access at global level

Once activating our plugins, go to Content Protection >> Restriction tab under your admin dashboard.

Simply enter the IP addresses which you want to bypass the protection provided by PPP Gold. Users from these IP addresses will be able to access all protected content on your site without having to log in.

Note that:

  • Only IPv4 addresses are supported
  • Asterisk (*) is used for will card matching, e.g. 1.1.1.* will match IP from 1.1.1.0 to 1.1.1.255

Whitelist IP access for individual page or post

Whitelisted IP addresses can also be configured on the individual level. You can specify different IP whitelists per page or disable this feature for certain pages.

Click on “Configure protection” button under “Content Protection” column in Pages or Posts screen.

It will bring up a window popup where you can protect this content and input IP addresses to whitelist.

You are offered 3 options including:

  • Use default setting
    • Follow the global-level whitelist in the plugin’s settings page
  • Disable IP Restriction
  • Allow these IP addresses only
    • Give the users from these IP addresses permission to access this content.

Note that:

  • Only IPv4 addresses are supported
  • Asterisk (*) is used for wildcard matching, e.g. 1.1.1.* will match IP from 1.1.1.0 to 1.1.1.255

You can whitelist IP access to not only pages and posts but also custom post types.

Let’s say you’re selling products specifically for the US market. You might want only users from the US to access the protected products on your site.

You just have to add Product types to Post Type Protection option under PPP Gold settings page before setting whitelisted IP addresses.

Similar to content protection, there are some cases where you want to make your protected media files visible to internal access. By whitelisting their IP addresses, you can grant them permission to access your protected files without any login requirement.

Requirements:

Whitelist IP access at global level

Once activating our plugins, go to Prevent Direct Access Gold >> Settings menu under your admin dashboard.

Switch to IP Restriction tab and navigate to “Restrict access to protected links” section as image below:

Add the list of IP addresses to which you want to grant access to all media files protected by PDA Gold. People coming from these IP addresses will be able to see all your protected files without logging into your website.

Whitelist IP access at file level

Whitelisted IP addresses can also be done at file level.

To do that, simply go to the Media library then click on “Configure file protection”.

It will bring up a window popup where you can protect, create download private links as well as configuring IP restriction for the file.

Under IP Restriction tab, you’ll see 3 options:

  • Use default settings: Follow the global-level IP restriction in the plugin’s settings page
  • Allow specific IP addresses: Allow users coming from specific IP addresses to access your file via protected link
  • Disable IP restriction: Disable function, whitelisted IP addresses in Setting page can’t access this file

You may want to share your protected files with certain people only. Apart from expiring the download links by date and clicks, you can also restrict its access to certain IP addresses. Similarly, you can blacklist certain IP addresses from accessing your private files instead.

Requirements:

Block IP access at global level

Once activating our plugins, go to Prevent Direct Access Gold >> Settings menu under your admin dashboard.

Switch to IP Restriction tab and navigate to “Restrict access to private download links” section as image below:

Here, add IP addresses that you want to block from accessing your private download links.

Blacklist these IP address from accessing private links for all files.

Block IP access at file level

Once activating our plugins, you will see a new “Prevent Direct Access” column under Media Library List View.

Simply click on “Configure file protection” button next to the file title of which you want to blacklist certain IP addresses.

It will bring up a window popup where you can protect this file, create download private links as well as prevent specific IP addresses from accessing these links.

Click “Submit” once you’re done.

Please note that individual configurations will work in parallel with global settings.

How to Grant IP Addresses Access to Protected Folders

Let’s say you’re protecting files using our Folder Protection. By rights, only users who logged in to your site with correct permission can view protected files.

What if you also want to allow users coming from specific IP addresses to access your files without logging in?

Simply add the following code snippet to your (child) theme’s functions.php file or Code Snippets plugin.

add_filter(

   'pda_ar_before_check_folder_protection',

   function ( $result, $folder_protection ) {

      if ( ! class_exists( 'Pda_Ip_Block_Services' ) ) {
         return $result;
      }

      $allowed_folders = [


         // Folders under WordPress uploads directory
         '2022' => [ 

             // Add specific or wildcard IPv4 addresses here
            'allowed_ips' => [ '127.0.0.1', '116.103.18.*' ],

         ],

          // Folders under WordPress root directory starting with ^
          '^sample'   => [
 
            // Add specific or wildcard IPv4 addresses here
            'allowed_ips' => [ '127.0.0.1', '116.103.18.*' ],

         ],

      ];

      $service    = new Pda_Ip_Block_Services();

      $ip_address = $service->get_ip_referer();

      if ( isset( $allowed_folders[ $folder_protection ] ) ) {

         $allowed_folder = $allowed_folders[ $folder_protection ];

         $allowed_ips    = $allowed_folder['allowed_ips'];

         if ( in_array( $ip_address, $allowed_ips) ) {

            return true;

         }

      }

      return $result;

   }, 50, 3

);

How to Restrict Access to Entire Website

This setting enables you to block all undefined IP addresses access to your whole site, from both backend and frontend.

Requirements:

Once activating our plugins, go to Prevent Direct Access Gold >> IP Restriction tab under your admin dashboard. You can find “Restrict access to the entire website” section at the bottom of this tab.

Simply enter those IP addresses that you want to access your website only.

Don’t forget to add and whitelist your current IP address. Otherwise, you can’t access the admin dashboard once logged out.

Please note that once you whitelist these IP addresses, the rest of the world won’t be able to access your website. They’ll see this message instead:

These 2 PDA Gold features work best on Apache servers. Please drop us a support email if you’re using other types of servers.
Lasted updated on October 6, 2022