• Resolved davidprt

    (@davidprt)


    Hi,

    I’d like to add exceptions for dynamic pages. I was trying this code according to the Github help:

    The webpage I’d like to add is:
    https://x.y.z/?page_id=2
    The following code works but also allows to see all pages with page_id without a login

    function my_forcelogin_whitelist( $whitelist ) {
    // whitelist URL if query string contains ‘parameter’
    if( isset($_GET[‘page_id’]) ) {
    $whitelist[] = site_url($_SERVER[‘REQUEST_URI’]);
    }
    // whitelist URL where ‘value’ is equal to query string ‘parameter’
    if( $_GET[‘page_id’] == ’20’ ) {
    $whitelist[] = site_url($_SERVER[‘REQUEST_URI’]);
    }
    return $whitelist;
    }
    add_filter(‘v_forcelogin_whitelist’, ‘my_forcelogin_whitelist’, 10, 1);
    ///////

    If I use the following code to restrict the filter only for the page_id=20 then I get my site broken.

    function my_forcelogin_whitelist( $whitelist ) {
    // whitelist URL where ‘value’ is equal to query string ‘parameter’
    if( isset($_GET[‘page_id’] == ’20’ )) {
    $whitelist[] = site_url($_SERVER[‘REQUEST_URI’]);
    }
    return $whitelist;
    }
    add_filter(‘v_forcelogin_whitelist’, ‘my_forcelogin_whitelist’, 10, 1);
    /////////////
    In both cases I put the code in the end of the functions.php file

    Could you tell me what am I doing wrong?

    Thank you

Viewing 1 replies (of 1 total)
  • Plugin Author Kevin Vess

    (@kevinvess)

    Ah, I see what the problem is. In your second example, you wrapped the isset() function around your comparison operator for the $_GET[‘page_id’] == ’20’ –?you can’t do that.
    https://php.net/manual/en/function.isset.php

    Try the following code instead:

    /**
     * Filter Force Login to allow exceptions for specific URLs.
     *
     * @return array An array of URLs. Must be absolute.
     **/
    function my_forcelogin_whitelist( $whitelist ) {
      // whitelist URL where 'value' is equal to query string 'parameter'
      if( isset($_GET['page_id']) && $_GET['page_id'] == '20' ) {
        $whitelist[] = site_url($_SERVER['REQUEST_URI']);
      }
      return $whitelist;
    }
    add_filter('v_forcelogin_whitelist', 'my_forcelogin_whitelist', 10, 1);
    • This reply was modified 8 years ago by Kevin Vess.
Viewing 1 replies (of 1 total)
  • The topic ‘Whitelist Dynamic URLs Error’ is closed to new replies.