5

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.

http://wiki.civicrm.org/confluence/display/CRMDOC/Javascript+Reference#JavascriptReference-UseonNon-CiviCRMPages

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?

5

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() {
    civicrm_initialize();
    $civi_data = civicrm_api( 'event', etc etc );
    $data = my_process_civi_data( $civi_data );
    echo json_encode( $data );
    exit();
}
| improve this answer | |
1

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.

CRM_Core_Resources::singleton()->addCoreResources('page-footer');

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.
| improve this answer | |

Your Answer

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

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