I am currently using a Wordpress plugin (Pagerestrict plugin which references URLS based on Page ID/Slugs)to restrict certain pages to logged in users only. This plugin works fine with standard wordpress URL structure but the issue I have is some of my pages which are directly linked to some plugin functionality have http://mywebsite.com/wp-content/plugins/purchase.php instead of a standard http://mywebsite.com/wp-content/plugins/purchase/ wordpress url structure. So what i tried in cases like this is to manually add the functionality below which didn't work

function my_page_template_redirect() {
            $url_path = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
            if( strpos($url_path, '/wp-content/plugins/purchase.php/') !== 0 && ! is_user_logged_in() )
                wp_redirect( wp_login_url() );
add_action( 'template_redirect', 'my_page_template_redirect' );

i also saw this https://wpexplorer-themes.com/total/snippets/restrict-content-access-user/ article which recommended something similar to what i was looking for but the only twist here is it matches page conditions with $page_id instead of the URL and since my URL (refer above) which is directly linked to plugin page is not generated via wp pages or posts it has no IDs or slugs. Is there a way to workaround this? Will appreciate some recommendations.

  • 1
    Why are you linking directly to files within your plugin? – Q Studio Jan 10 at 21:28
  • @Q Studio Good question. I know its not good practice. It is a wordpress AD plugin url with purchase page directly linked to the plugin, dont know why it is designed that way. I even tried to redirect it first to /URL/ structure my .htaccess before implementing the above but no success. This page is what i have to show at the frontend in order to have people choose an ad slot to purchase which is why i needed it restricted. My plan is to secure it or use developer.wordpress.org/reference/functions/auth_redirect once the right mechanisms are in place. – john zuh Jan 10 at 21:58

The template redirect is most useful for managing the output of theme files. To redirect a non-logged in user when the page loads, attach your function to the 'init' hook. (If you call it sooner than that in the stack, you may get an error because the function definition hasn't been included yet.)

In short, if you replace the action hook in the code you provided with:

add_action( 'init', 'my_page_template_redirect' );

it should achieve what you're trying to accomplish.

Reference: https://developer.wordpress.org/reference/functions/is_user_logged_in/

  • Your suggestion seems to have worked but I end up having everything redirected instead of that particular page url. Could it be that my condition here ->>. if ( strpos($url_path, '/wp-content/plugins/purchase.php/') isnt working properly? – john zuh Jan 10 at 23:01
  • @johnzuh You are correct! I hadn't seen that before, but strpos returns boolean false when the string is not found, so you'll want to change the !==0 to say !== false instead. php.net/manual/en/function.strpos.php – nightowl Jan 10 at 23:08
  • @nighowl changing that value did the trick. – john zuh Jan 13 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.