Community Forums › Forums › Archived Forums › Design Tips and Tricks › Restrict access to page template for certain types of users
- This topic has 22 replies, 4 voices, and was last updated 8 years, 10 months ago by James.
-
AuthorPosts
-
June 7, 2015 at 1:01 pm #155246tsahilMember
Hi,
I am trying to restrict the access to specific page template so that only a specific type of users in my site can gain access to it.
For those who aren't that user type, I want to show a message and request they login instead.
How do I go about doing that in a Genesis Child theme template?
June 7, 2015 at 7:54 pm #155293JamesParticipanthi there
i did a site some time ago (and about to do another one)
at the time i used these plugins for various functionality around the site.
June 8, 2015 at 9:41 am #155378tsahilMemberThanks Jamie.
These look like overkill to what I am looking for.
I just need the code snippet to put in a single page template - putting a bloated members plugin is the last thing I want to do at the moment...June 8, 2015 at 3:24 pm #155428JamesParticipantoh, ok
so you need a conditional maybe?
I was thinking you wanted a full login thing
how will people access this page, just a link, and what will happen if they aren't the right user, redirect...?
June 9, 2015 at 12:02 am #155496Brad DaltonParticipantTry
if ( ! current_user_can( ) ) { return; }
https://codex.wordpress.org/Function_Reference/current_user_can
June 9, 2015 at 1:08 am #155506JamesParticipantnice call Brad, that should work.
June 9, 2015 at 1:15 am #155508Brad DaltonParticipantThanks. Use it on my own site Jamie and hard coded the login form into the template as well.
You could also add
wp_login_url( $redirect );
to the function as another option.
June 9, 2015 at 2:03 am #155520Brad DaltonParticipantAnother option is to use short codes to protect content for specific user levels only however users will need to be logged in regardless.
June 9, 2015 at 2:04 am #155521tsahilMemberThanks all.
I probably wasn't clear enough with my problem. I know how to check the user's type.
My problem is in finding which filter to add and how to start it off.I am writing a page template in my child theme.
It looks like this for the most part:<?php /* Template Name: Book Page */ ?> <?php add_filter('genesis_pre_get_option_site_layout', '__genesis_return_full_width_content'); genesis(); ?>
What additional filter should I add and what should I place in it in order for the following to happen:
1. If a user is an admin or a certain type (new custom type that I added), then he should see the content.
2. If the user doesn't fit the criteria above, I show him something like "This page is only available for people who subscribe for ABC. Please login using the form below".Thanks a bunch.
June 9, 2015 at 2:17 am #155526JamesParticipantyeh that's what i figured you meant, but i can't find a conditional for only a certain user, only all users logged in.
and WordPress would only be able to tell if it was a certain user if they were logged in, if they were not already logged in, how will WordPress know?
tricky
something like this would be a starting point
if ( is_user_logged_in() ) { // do something here like show the page } else { // do something like show a message }
but again, they would need to be logged in,
can't you just password protect the page?
June 9, 2015 at 2:21 am #155528tsahilMemberI don't have an issue knowing the user. For that, I used the following condition:
if($user>0 && get_user_meta($user,'premium_subscriber',true))
What I am looking for is to be able to place it in the page template at the correct place - somewhere that comes before WordPress writes the content of the page itself and enables me to replace it with some other text.
As for password protection, this won't work, as I have a few pages like that and I want the users to be able to move between them without needing to click passwords all over the place.
June 9, 2015 at 2:26 am #155531Brad DaltonParticipantLink to the page template please. Can you also paste the code you have so far in a Github Gist so we can take a look.
June 9, 2015 at 2:35 am #155532JamesParticipanti completely understand
you have content on the page that you only want to show to logged in users of a certain type, else, show a message.
i'll have to think on this one for a bit.
June 9, 2015 at 2:36 am #155534JamesParticipantsorry Brad
i didn't see you post above
i think you would be better with this one. needs a PHP Jedi
June 9, 2015 at 2:53 am #155537tsahilMemberHere you go: https://gist.github.com/anonymous/e15c9a615f938889b550
I am not sure what filter to add/remove to get what I need done into this file.
June 9, 2015 at 9:09 am #155573Brad DaltonParticipantJune 9, 2015 at 11:56 am #155598tsahilMemberWhy not?
I still want my template to appear - the header, the footer, the whole page.
I just want to replace the content with an explanation and a short login form (or a link to login).June 9, 2015 at 3:32 pm #155623JamesParticipantyou might need to hire someone to do this for you (not me)
you are asking for a rather difficult task for a forum, and we are all trying to solve it for you but what you require is a lot more complicated than it sounds.
June 9, 2015 at 5:06 pm #155637Erik D. SlaterMember@braddalton, @jamie-mitchell: I'm feeling your pains, boys ... and I have no idea why your proposals haven't been considered as acceptable here, one way or the other 🙂
@tsahil: You should be able to handle this using a custom shortcode. Brad already suggested this ... but since I'm about to do something similar for my custom theme, I'd like to see if the solution I'm about to start coding for myself will work for you too 🙂 It may well be that you have something in mind that you haven't fully articulated or disclosed on this thread.Just for completeness, can you provide a link to your site?
Erik D. Slater: Digital Platform Consultant • LinkedInJune 9, 2015 at 5:58 pm #155645Brad DaltonParticipantThe
genesis();
function contains all the hooks for markup.
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.