Body: <p>I am attempting to interact with my CiviCRM backend using the AJAX API. I created a 'contact' page in WordPress, referencing a profile I created solely to load the CiviCRM javascript into my custom page. The template is part of a custom theme I have created from scratch.</p>
  
  <p>To test, I can open the contact page in my browser and drop this into the console:</p>
  
  <pre><code>CRM.api3('Contact', 'create', {
    contact_type : 'Individual',
    first_name : 'Test',
    last_name : 'Aroonie',
    email : 'testaroonie@verizonsucks.net',
    note : 'Hello I am contacting you thx'
  });
  </code></pre>
  
  <p>This gives me an OK response and creates a new contact as I expect, <em>when I'm logged in to wp-admin</em>. However, when I try this in an incognito window, I get an HTML response saying "You do not have permission to access this page."</p>
  
  <p>I read the <a href="https://docs.civicrm.org/dev/en/latest/security/permissions/" rel="nofollow noreferrer">permissions documentation</a>, which recommends implementing <code>hook_civicrm_alterAPIPermissions()</code> to override contact-create permission, which I have done in my theme's <code>functions.php</code>. It doesn't look like this hook is being called.</p>
  
  <p>Am I on the right track here? If so, what details have I missed? </p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-11-27T04:04:08.060Z
Comments.Text: This feels like a hack, but after learning more about how WP hooks work, my solution was to add the following to my `functions.php` file:
  ```function allow_public_create_contact($entity, $action, &$params, &$permissions) {
      if ($entity == 'contact' && $action == 'create') {
        $permissions['contact']['create'] = array();
    }
  }
  add_filter('civicrm_alterAPIPermissions', 'allow_public_create_contact', 10, 4);```
Comments.UserDisplayName: asciimo
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-11-27T05:55:06.517Z
Comments.Text: If it works, then definitely post it as an answer :)
Comments.UserDisplayName: Jon G - Megaphone Tech
Tags: <contacts><profile><permissions><ajax>
Title: How can I use the AJAX API to create a Contact Us form?
ThreadText: # How can I use the AJAX API to create a Contact Us form?
  
  (Asked by: username_0 on 2017-11-27)
  
  I am attempting to interact with my CiviCRM backend using the AJAX API. I created a 'contact' page in WordPress, referencing a profile I created solely to load the CiviCRM javascript into my custom page. The template is part of a custom theme I have created from scratch.
  
  To test, I can open the contact page in my browser and drop this into the console:
  
  ```
  CRM.api3('Contact', 'create', {
    contact_type : 'Individual',
    first_name : 'Test',
    last_name : 'Aroonie',
    email : 'testaroonie@verizonsucks.net',
    note : 'Hello I am contacting you thx'
  });
  
  ```
  
  This gives me an OK response and creates a new contact as I expect, *when I'm logged in to wp-admin*. However, when I try this in an incognito window, I get an HTML response saying "You do not have permission to access this page."
  
  I read the [permissions documentation](https://docs.civicrm.org/dev/en/latest/security/permissions/), which recommends implementing `hook_civicrm_alterAPIPermissions()` to override contact-create permission, which I have done in my theme's `functions.php`. It doesn't look like this hook is being called.
  
  Am I on the right track here? If so, what details have I missed?
  
  **Tags:** contacts, profile, permissions, ajax
  
  > **Comments:**
  > **username_0**: This feels like a hack, but after learning more about how WP hooks work, my solution was to add the following to my `functions.php` file: ```function allow_public_create_contact($entity, $action, &$params, &$permissions) {     if ($entity == 'contact' && $action == 'create') {       $permissions['contact']['create'] = array();   } } add_filter('civicrm_alterAPIPermissions', 'allow_public_create_contact', 10, 4);```  
  > **username_1**: If it works, then definitely post it as an answer :)  
  
  ---
  
  ## 0 answers
  
  