Answers.Body: <p>The answer davejenx is correct but <a href="https://issues.civicrm.org/jira/browse/CRM-19028" rel="noreferrer">no longer considered best practice</a>.  Instead use:</p>
  
  <pre><code>CRM_Core_Session::singleton()-&gt;getLoggedInContactID();
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-05T12:41:41.480Z
Answers.Text: Thanks Jon. Which Civi versions implement this method?
Answers.UserDisplayName: davejenx
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-05T20:45:55.253Z
Answers.Text: It's in 4.4+, maybe earlier versions too?
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-03T21:11:00.730Z
Answers.Text: I have some custom code using this and it recently stopped working (it's returning 0). I've converted to using the UFMatch api call to fix. This is as of 5.19.4 with Drupal 7.
Answers.UserDisplayName: Alan Dixon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-04T16:51:24.593Z
Answers.LastActivityDate: 2016-07-04T16:51:24.593Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.Body: <p><em>Edit:</em> The techniques below get the <em>logged-in user's</em> contact id. However the questioner has clarified that this isn't what they meant by "the current contact id". I'll leave this answer in place in case it's useful to someone else who misunderstood the question.
  Here's a simple way:</p>
  
  <pre><code>CRM_Core_Session::singleton()-&gt;getLoggedInContactID();
  </code></pre>
  
  <p>The above (for which thanks to Jon G) replaces this older technique:</p>
  
  <pre><code>$session = CRM_Core_Session::singleton();
  $contact_id = $session-&gt;get('userID');
  </code></pre>
  
  <p>You can also do it by getting the user id from the CMS (Drupal / Wordpress / Joomla), then using the CiviCRM API to get that user's UFMatch record, which gives you the contact id.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-04T16:50:33.120Z
Answers.Text: This is no longer considered best practice, see my answer.
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-09T14:01:38.133Z
Answers.Text: I use wordpress. Do you know how to return the current contact_id in wordpress?
Answers.UserDisplayName: Csaba
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-09T14:07:07.440Z
Answers.Text: This will return MY wordpress username's ID but I need the currently displayed contact's civicrm ID :)
Answers.UserDisplayName: Csaba
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-09T14:30:41.810Z
Answers.Text: Like davejenx said: use the Civi API UFMatch Get (with uf_id = the wordpress username's id), it will return you the contact_id.
Answers.UserDisplayName: ErikH - CiviCooP
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-09T14:46:07.893Z
Answers.Text: Ah, I misunderstood what you were trying to do. In that case, see Coleman's answer.
Answers.UserDisplayName: davejenx
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-09T13:46:44.917Z
Answers.LastActivityDate: 2016-07-07T08:22:25.177Z
Answers.LastEditDate: 2016-07-07T08:22:25.177Z
Answers.UserDisplayName: davejenx
Answers.Body: <p>If you are on the contact summary screen, then the contact id is always <code>$_GET['cid']</code>. To give yourself peace-of-mind about using raw input, cast it to an integer.</p>
  
  <pre><code>$contactId = (int) $_GET['cid'];
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-10T09:03:01.723Z
Answers.Text: You could also do this with    `$contactId = CRM_Utils_Request::retrieve('cid', 'Positive');`
Answers.UserDisplayName: ErikH - CiviCooP
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-14T20:14:31.237Z
Answers.Text: I think Erik's should be a separate answer and the only correct one. Coleman, really!
Answers.UserDisplayName: Alan Dixon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-09T14:39:19.873Z
Answers.LastActivityDate: 2015-07-09T15:55:21.173Z
Answers.LastEditDate: 2015-07-09T15:55:21.173Z
Answers.UserDisplayName: Coleman
Body: <p>I display the summary page for a civicrm contact and I need to get the current contact id. Is there any built in function for that?</p>
  
  <p>I have this code where I explicitly refer to the civicrm id 4. Instead of doing that I would like to refer to the current civicrm contact.</p>
  
  <pre><code>$contact_id = 4;
  $custom_name = array("custom_2","custom_3","custom_6","custom_7");
  $params = array(
          'return' =&gt; $custom_name,
          'version' =&gt; 3,
          'id' =&gt; $contact_id,
  );
  
  $result = civicrm_api( 'contact','get',$params );
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-04-03T23:31:16.467Z
Comments.Text: Related question asks if there's an API method to do the same: http://civicrm.stackexchange.com/questions/10849/how-to-get-contact-id-of-logged-in-user-in-js-api
Comments.UserDisplayName: Chris Burgess
Tags: <api>
Title: Get current contact id?
ThreadText: # Get current contact id?
  
  (Asked by: username_0 on 2015-07-09)
  
  I display the summary page for a civicrm contact and I need to get the current contact id. Is there any built in function for that?
  
  I have this code where I explicitly refer to the civicrm id 4. Instead of doing that I would like to refer to the current civicrm contact.
  
  ```
  $contact_id = 4;
  $custom_name = array("custom_2","custom_3","custom_6","custom_7");
  $params = array(
          'return' => $custom_name,
          'version' => 3,
          'id' => $contact_id,
  );
  
  $result = civicrm_api( 'contact','get',$params );
  
  ```
  
  **Tags:** api
  
  > **Comments:**
  > **username_1**: Related question asks if there's an API method to do the same: http://civicrm.stackexchange.com/questions/10849/how-to-get-contact-id-of-logged-in-user-in-js-api  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_6 on 2015-07-09. Score: 9 | Accepted answer
  
  If you are on the contact summary screen, then the contact id is always `$_GET['cid']`. To give yourself peace-of-mind about using raw input, cast it to an integer.
  
  ```
  $contactId = (int) $_GET['cid'];
  
  ```
  
  > **Comments:**
  > **username_5**: You could also do this with    `$contactId = CRM_Utils_Request::retrieve('cid', 'Positive');`  
  > **username_4**: I think Erik's should be a separate answer and the only correct one. username_6, really!  
  
  ---
  
  ### Answer by username_2 on 2016-07-04. Score: 18
  
  The answer username_3 is correct but [no longer considered best practice](https://issues.civicrm.org/jira/browse/CRM-19028). Instead use:
  
  ```
  CRM_Core_Session::singleton()->getLoggedInContactID();
  
  ```
  
  > **Comments:**
  > **username_3**: Thanks Jon. Which Civi versions implement this method?  
  > **username_2**: It's in 4.4+, maybe earlier versions too?  
  > **username_4**: I have some custom code using this and it recently stopped working (it's returning 0). I've converted to using the UFMatch api call to fix. This is as of 5.19.4 with Drupal 7.  
  
  ---
  
  ### Answer by username_3 on 2015-07-09. Score: 7
  
  *Edit:* The techniques below get the *logged-in user's* contact id. However the questioner has clarified that this isn't what they meant by "the current contact id". I'll leave this answer in place in case it's useful to someone else who misunderstood the question.
  Here's a simple way:
  
  ```
  CRM_Core_Session::singleton()->getLoggedInContactID();
  
  ```
  
  The above (for which thanks to Jon G) replaces this older technique:
  
  ```
  $session = CRM_Core_Session::singleton();
  $contact_id = $session->get('userID');
  
  ```
  
  You can also do it by getting the user id from the CMS (Drupal / Wordpress / Joomla), then using the CiviCRM API to get that user's UFMatch record, which gives you the contact id.
  
  > **Comments:**
  > **username_0**: I use wordpress. Do you know how to return the current contact_id in wordpress?  
  > **username_0**: This will return MY wordpress username's ID but I need the currently displayed contact's civicrm ID :)  
  > **username_5**: Like username_3 said: use the Civi API UFMatch Get (with uf_id = the wordpress username's id), it will return you the contact_id.  
  > **username_3**: Ah, I misunderstood what you were trying to do. In that case, see username_6's answer.  
  > **username_2**: This is no longer considered best practice, see my answer.  
  
  ---
  
  