0

I am developing a page in WordPress. In functions.php file I have this:

function feed_add_notmusa() {
  add_feed('mypage', 'mypage_function');
}

function mypage_function() {
  get_template_part('/mypage');
}

But how can I restrict access to /mypage so only logged in users can access?

Could you please help me?

1
  • 1
    get_template_part doesn't take a URL path as a parameter, it takes a php template file name, the / is not needed. It will try to load childtheme/mypage.php and then parenttheme/mypage.php if it doesn't exist
    – Tom J Nowell
    Commented Dec 3, 2018 at 21:48

1 Answer 1

1

Try this: is_user_logged_in

function mypage_function() {
  if( is_user_logged_in() ) {
    get_template_part('mypage');
  }else{
    echo 'please login for awesomeness';
  }
}
4
  • thanks for your help, it works ok, is there some way to restring for role to only admin can access to that page? Commented Dec 3, 2018 at 21:17
  • @skycomputer2 yes, but that's a new question, click "Ask Question" and include the solution here and what you need to do, and remember, we're all volunteers using our own personal time, the answer you get may not be in the form of copy pasteable code, but rather an explanation of what you would do
    – Tom J Nowell
    Commented Dec 3, 2018 at 21:50
  • In general we use the user_can function & cross-reference with the Roles_and_Capabilities documentation to figure out what type of user we're dealing with... SO it's less user-level detection & more user-capability detection, because you can always add a custom user role with a custom set of abilities. But like @TomJNowell said, that would be a different question. Also, it helps greatly if you provide a... Minimum Complete & Verifyable Example
    – admcfajn
    Commented Dec 3, 2018 at 23:14
  • Another post showing how to check roles with current_user_can: wordpress.stackexchange.com/questions/131814/… Commented Dec 3, 2018 at 23:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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