WordPress.org

Forums

[resolved] is_page within a plugin (2 posts)

  1. tomkinsg
    Member
    Posted 7 years ago #

    Hi, diving into updating a plugin - Members only - so that it only requires login for a pages. The original code looks like this

    function members_only()
    {
    	global $userdata, $members_only_opt, $members_only_reqpage;
    
    	$home = get_bloginfo('url'); //Get base URL or WordPress install
    	$currenturl = sprintf('http%s://%s%s',(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == TRUE ? 's': ''), $_SERVER['HTTP_HOST'], $members_only_reqpage); //Get the current URL
    
    	//Check redirection settings
    	if ($members_only_opt['redirect_to'] == 'login' || $members_only_opt['redirect_to'] == 'specifypage' && $members_only_opt['redirect_url'] == '') //If redirecting to login page or specified page is blank
    	{
    		$redirect = "/wp-login.php";
    		if ($members_only_opt['redirect'] == TRUE) //If redirecting to original page after logging in
    		{
    			$redirect .= "?redirect_to=";
    			$redirect .= $members_only_reqpage;
    		}
    	}
    	elseif ($members_only_opt['redirect_to'] == 'specifypage' && $members_only_opt['redirect_url'] != '') //If redirecting to specific page
    	{
    		$redirect = '/'.$members_only_opt['redirect_url'];
    	}
    
    	//Create Redirection URL
    	$redirection = $home.$redirect;
    
    	if ($userdata->ID == '' && $members_only_opt['members_only'] == TRUE)//Check if user is logged in and blog is Members Only
    	{
    		//Check we aren't already at the page we're redirecting to (with and without the trailing slash) or wanting to go to the login page, registration page or somewhere in wp-admin
    		if ($currenturl == $redirection || $currenturl == $redirection.'/' || preg_match("/wp-login.php/i", $_SERVER["REQUEST_URI"]) || preg_match("/wp-register.php/i", $_SERVER["REQUEST_URI"]) || preg_match("/wp-admin/i", $_SERVER["REQUEST_URI"]))
    		{
    			//Do Nothing
    		}
    		else
    		{
    			//Redirect Page
    			ob_start();
    			header("Location:".$redirection);
    			ob_end_flush();
    		}
    
    	}
    	else
    	{
    		//Do Nothing
    	}
    
    }

    All I am doing is putting in a test to see if it is a page...

    {
    	if (is_page())
    	{
    		//Redirect Page
    		ob_start();
    		header("Location:".$redirection);
    		ob_end_flush();
    	}
            else
            {
                    // Do nothing
            }
    }

    The is_page() function just always appears to return false.

    any pointers.

    thanks

  2. tomkinsg
    Member
    Posted 7 years ago #

    I basically gave up with this. Don't know why i could not get it to work.

    Instead I borrowed another plugin (Angsuman's authenticated wordpress plugin) and tweaked it to do what i needed. If i get time, I will clean it up an publish it as I think it may have a broader application and I don't think any of the other authentication plugins do authentication of a subset of a site without the need for entitlements to be set up.

    thanks
    G

Topic Closed

This topic has been closed to new replies.

About this Topic