I've taken a look at the docs here under "Use on Non-CiviCRM Pages" but I'm coming up empty when trying to access the CRM object via JavaScript.


In functions.php I've tried this:

civicrm_initialize(); $manager = CRM_Core_Resources::singleton(); $manager->addCoreResources(); $manager->addScriptUrl(path-to-ajax.js); // looking around through the available scripts this seemed the obvious candiate

I can get it to load scripts via wp_enqueue_script, but I have no idea which scripts it needs. The docs appear to suggest that it should just magically load everything into the header but that isn't the case for me. The CRM object is never defined.

What I want to do is grab the event list in JSON format.

Any clues? What am I missing?


2 Answers 2


One way would be to write your own AJAX handler in the usual WordPress way and then grab the event list via the CiviCRM API. Untested pseudocode:

// add AJAX functionality
add_action( 'wp_ajax_my_civi_get_event_data', 'my_civi_get_event_data' );
add_action( 'wp_ajax_nopriv_my_civi_get_event_data', 'my_civi_get_event_data' );

 * Get data from CiviCRM
function my_civi_get_event_data() {
    $civi_data = civicrm_api( 'event', etc etc );
    $data = my_process_civi_data( $civi_data );
    echo json_encode( $data );

Not perfect (see below), but I've made this work in a pinch:

As of civicrm version 5.17 at least, addCoreResources($region) works if you specify 'page-footer' for $region.


The function signature for addCoreResources() says addCoreResources('html-header'), and some quick testing shows that CiviCRM is for some reason not able to add resource files to that region. For example, try it with CRM_Core_Resources::singleton()->addScriptFile('com.example.extensionname', 'js/example.js', $weight=1, 'html-header') and you'll see your file is not included; other other hand, if that 4th argument is left blank (it defaults to 'page-footer') your file will be included.

So by specifying the 'page-footer' region for the core resources, they are included.

Other thoughts:

  • I'm unsure what other regions may be provide the same effect;
  • I'm unsure what negative side-effects may come from putting the core resource files in a region other than 'html-header'; at the very least, it does dump some JS errors into your console, which is obviously less than ideal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.