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() {

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

Could you please help me?

  • 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
    Dec 3 '18 at 21:48

Try this: is_user_logged_in

function mypage_function() {
  if( is_user_logged_in() ) {
    echo 'please login for awesomeness';
  • thanks for your help, it works ok, is there some way to restring for role to only admin can access to that page? Dec 3 '18 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
    Dec 3 '18 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
    Dec 3 '18 at 23:14
  • Another post showing how to check roles with current_user_can: wordpress.stackexchange.com/questions/131814/… Dec 3 '18 at 23:29

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.