Restrict URL not working for Membership 2 Pro

I believe that I have the URL restriction set according to the directions so that anything underneath /dowload would require membership. However, I am able to access the URL's within this directory without being logged in.
The main /download page is secured through the default plugin but the URL security doesn't seem to have done anything.

  • chris
    • Site Builder, Child of Zeus

    After speaking with Jude (who was very helpful) we found that the downloads plugin being used generates my download pages on the fly - AFTER the M2P protection rules are applied.
    His suggestion was to ask for a small custom snippet of code that would redirect all non-logged in users to the main downloads page whenever they access a URL download/*
    He also mentioned using the M2P API and detecting membership before directing but honestly, I've no clue how to do any of this.

    This small snippet/workaround could help us and prevent anyone from sharing a download URL with non-members - a big deal for us given the sensitivity of some of the files that will be shared on our site.

    Can you help with this, please?

  • Jude
    • DEV MAN

    Hey again @chris

    Here you go

    add_action( 'template_redirect', 'redirect_to_specific_page' );
    
    function redirect_to_specific_page() {
    
    if ( strpos($_SERVER['REQUEST_URI'], 'downloads') !== false  && ! is_user_logged_in() ) {
    
    wp_redirect( 'http://www.example.dev/downloads/', 404 );
      exit;
        }
    }

    Just copy/paste the code into the functions.php file of your child theme or a site specific plugin if you use one. Oh and replace example.com with your domain name

    Cheers
    Jude

  • Jude
    • DEV MAN

    Sorry about that. Can you try with this code instead ?

    add_action( 'template_redirect', 'redirect_to_specific_page' );
    
    function redirect_to_specific_page() {
    
    if ( preg_match("/downloads\/.(.*)/",$_SERVER['REQUEST_URI'], )  && ! is_user_logged_in() ) {
    
    wp_redirect( 'http://www.example.dev/downloads/', 404 );
      exit;
        }
    }

    Jude

  • Michael Bissett
    • Recruit

    Hey @chris, Michael here! :slight_smile:

    Could you please send in the following via our secure contact form, so that we can dig into this further:

    - Mark to my attention, the subject line should contain only: ATTN: Michael Bissett
    - Do not include anything else in the subject line, doing so may delay our response due to how email filtering works.
    - Link back to this thread
    - Include WordPress admin access details (login address, username & password)
    - Include SFTP log-in details (hostname, username, password, port number)
    - Include any relevant URLs for your site (like a URL for one of your downloads)

    IMPORTANT: Please make sure you select "I have a different question" for your topic, so it doesn't go back to the forums - this and the subject line ensure that it gets assigned to me.

    https://premium.wpmudev.org/contact/

    Thanks! :slight_smile:

    Kind Regards,
    Michael

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.