Forum
PHP -check if a page is restricted to members
Hi,
I'm using the members app (v1.6.1). On a particular master page, I'd like a way to check whether the page instance is restricted to members (with any tag), so as to select whether to include perch_layout('global/social-share');
. (The master page is otherwise identical for some pages outside of the members' area).
Is there a way I can write an if
statement with its condition being whether the "Restrict access to members with tags" field is empty? Or is there a better way?
Thanks
Hello Dave,
If you want to determine whether a member is logged in, you can use
perch_member_logged_in()
. If you want to check whether a member has a certain tag, you can useperch_member_has_tag()
. Use what's appropriate to your case to determine whether the visitor/member should have access.Documentation:
Hi Hussein,
Thanks but I don't think those functions cover what I need.
I have a master page and when it's used for pages inside the members area I want to exclude a line from the page. That way the social sharing buttons won't show for members-only pages, but will always display for other pages, no matter whether a user is logged in.
Maybe I'm missing something obvious?
Oh I see. Perhaps you can achieve this with page attributes?
sharing.html:
Add the following line to your
templates/pages/attributes/default.html
:In your master page:
Go to your page and click on the Details tab. At the bottom there should be a new checkbox. Unless it's checked, the
global/social-share
layout won't be included.Thanks very much for that.
Might there be a method where the social sharing links automatically won't be included when the page is for members only? E.g. check whether the "Restrict access to members with tags" field is empty.
Of course you can. Change the logic of the above solution to your needs.
Or you can use the
default
attribute to have the checkbox checked by default:This way it's checked for all pages by default, and you only have to uncheck it for the pages where you don't want to include the layout.
Sorry what I mean is:
When the user creates a members-only page in the admin, by filling in the "Restrict access to members with tags" field, rather than them also having to uncheck the checkbox for social sharing, is there instead a way to automatically detect (in an
if
) whether the "Restrict access to members with tags" field is empty? (I.e. bypass page attributes.)Maybe it's a long shot but thought I'd ask
To know whether the checkbox field is unchecked you still need to call
perch_page_attribute()
Or:
Not sure if this answers your question.
Is there an API method to tell whether a page has any member tags? If so, I could use this in the
if
statement. That way, rather than using page attributes, the person creating the page wouldn't have to select whether to display social sharing links.If not, I'll go ahead and use page attributes
I may have misunderstood your question. It is not entirely clear to me what you are trying to achieve. Do you want to restrict:
Either way it seems we have come back to
perch_member_logged_in()
andperch_member_has_tag()
functions.Hope this helps.
In the settings tab for a page, the user can enter tags into a field marked "Restrict access to members with tags". (Obviously this is standard for the members app.)
I'd like a way to check whether the page is restricted to members -with any tag. That way, when the master page is used for a page in the members-only area, I can use an
if
to hide the social sharing links.I feel like I could me missing an obvious solution but that's what I'm after.
Something like:
If this isn't possible, I assume the two options are
It sounds like you probably need to make a relatively simple app that creates one runtime function to look at the
pageAccessTags
column in theperch_pages
table - if there is something in there for the page you're on, return true. Use that result to decide if the social links show.Essentially, there's nothing built in to do that, as far as I'm aware.
EDIT: I should add that there is no public API available for this, so you would be going off piste a bit.
Ok it sounds to me you are trying to define tags at the page level and don't want to hard-code it in the master page. For example, some pages might be restricted to members with 'subscriber' tag, other pages may be restricted to members with 'premium' tag and by default there is no restriction. And all these pages share a single master page. Is this correct?
If so, I think you can still use page attributes for this.
page.php:
As far as I'm aware
perch_member_has_tag()
checks against a single tag, so the above works for a single tag.Or if you want to check whether a tag was entered in the settings rather than checking whether the logged-in member has entered the tag, i.e. this is to check whether the "page has tags":
Checking a checkbox is one click. Entering tags require more input from the user and is prone to error.
If you know you always check against one specific tag, you can hard-code it in the master page, use a checkbox field in your page attributes and rather than using a label like 'Hide sharing links' you can use 'Restrict to members' or 'Restrict to members with subscriber tag', so it feels "automatic" to the user.
There are various ways to handle this including building your own app like Duncan suggested. Hopefully you now have a good idea how to tackle this.
Yes.
The field marked "Restrict access to members with tags" seems to come as standard with the members app, and if there is anything in that field, the page should not show social shares.