Answers.Body: <p>Well maybe somebody has a better way of doing this, but this is how I ended up accomplishing this...</p>
  <p>Using the buildForm hook, I checked the Contact ID of the user accessing the form, and if there was no Contact ID then they were not logged, so I then used jQuery to completely replace the form on the page with the text &quot;You do not have permission to view this page!&quot;</p>
  <p>Code looks like this:</p>
  <pre><code>// Check if it is a Profile form with specific id (15 in this case)
  if ( $formName == 'CRM_Profile_Form_Edit' &amp;&amp; $form-&gt;getVar('_gid') == 15 ) {
  
    // What user is trying to access the form?
    $whatUser = civicrm_api3('Contact', 'get', [
        'sequential' =&gt; 1,
        'return' =&gt; [&quot;id&quot;],
        'id' =&gt; &quot;user_contact_id&quot;,
      ]);
  
    // Check if user id is set
    if ( isset($whatUser['id']) ) {
      // User id is set so a user must be logged in
      $okToShowPage = true;
    } else {
      // No user id so user is not logged in
      $okToShowPage = false;
    }
    
    if ($okToShowPage) {
      // Just let the profile page display as usually and do nothing.
      
    } else {
      // Completely delete DIV with id=crm-container and replace it with a new DIV
      CRM_Core_Region::instance('page-body')-&gt;add( ['script' =&gt; &quot;cj('#crm-container').replaceWith('&lt;div&gt;&lt;b&gt;You do not have permission to view this page.&lt;/b&gt;&lt;/div&gt;');&quot;] );
    }
  
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-30T02:37:58.733Z
Answers.LastActivityDate: 2022-07-30T02:37:58.733Z
Answers.UserDisplayName: jsherk
Body: <p>When I setup a new Profile, it is available to everybody publicly that has the link but I want to limit access to only logged in users (or even users in a specific group).</p>
  <p>I think this should be simple but for some reason I am missing how to do this.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-07-24T20:04:11.737Z
Comments.Text: I have removed the webform-civicrm tag from this post as you have added WP and they are mutually exclusive (you weren't to know) as webform-civicrm is a Drupal module.
Comments.UserDisplayName: petednz - fuzion
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-07-24T20:12:54.633Z
Comments.Text: for several of your questions it seems like you may be pushing the limit for Profiles. In drupal we can work around those limitations with webform-civicrm and views (for displaying info). I wonder if you need to investigate SearchKit and Afform to get more of what you are after, rather than the rather simpler concept of Profiles
Comments.UserDisplayName: petednz - fuzion
Tags: <wordpress><profile><forms>
Title: How to setup a profile page that is only visible to logged in users?
ThreadText: # How to setup a profile page that is only visible to logged in users?
  
  (Asked by: username_0 on 2022-07-24)
  
  When I setup a new Profile, it is available to everybody publicly that has the link but I want to limit access to only logged in users (or even users in a specific group).
  
  I think this should be simple but for some reason I am missing how to do this.
  
  **Tags:** wordpress, profile, forms
  
  > **Comments:**
  > **username_1**: I have removed the webform-civicrm tag from this post as you have added WP and they are mutually exclusive (you weren't to know) as webform-civicrm is a Drupal module.  
  > **username_1**: for several of your questions it seems like you may be pushing the limit for Profiles. In drupal we can work around those limitations with webform-civicrm and views (for displaying info). I wonder if you need to investigate SearchKit and Afform to get more of what you are after, rather than the rather simpler concept of Profiles  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2022-07-30. Score: 1 | Accepted answer
  
  Well maybe somebody has a better way of doing this, but this is how I ended up accomplishing this...
  
  Using the buildForm hook, I checked the Contact ID of the user accessing the form, and if there was no Contact ID then they were not logged, so I then used jQuery to completely replace the form on the page with the text "You do not have permission to view this page!"
  
  Code looks like this:
  
  ```
  // Check if it is a Profile form with specific id (15 in this case)
  if ( $formName == 'CRM_Profile_Form_Edit' && $form->getVar('_gid') == 15 ) {
  
    // What user is trying to access the form?
    $whatUser = civicrm_api3('Contact', 'get', [
        'sequential' => 1,
        'return' => ["id"],
        'id' => "user_contact_id",
      ]);
  
    // Check if user id is set
    if ( isset($whatUser['id']) ) {
      // User id is set so a user must be logged in
      $okToShowPage = true;
    } else {
      // No user id so user is not logged in
      $okToShowPage = false;
    }
    
    if ($okToShowPage) {
      // Just let the profile page display as usually and do nothing.
      
    } else {
      // Completely delete DIV with id=crm-container and replace it with a new DIV
      CRM_Core_Region::instance('page-body')->add( ['script' => "cj('#crm-container').replaceWith('<div><b>You do not have permission to view this page.</b></div>');"] );
    }
  
  }
  
  ```
  
  
  ---
  
  