1

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, 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?

  • 1
    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); – asciimo Nov 27 '17 at 4:04
  • If it works, then definitely post it as an answer :) – Jon G - Megaphone Tech Nov 27 '17 at 5:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.