Answers.Body: <p>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:</p>
  
  <pre><code>// 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();
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-19T10:32:28.683Z
Answers.LastActivityDate: 2015-05-19T10:32:28.683Z
Answers.UserDisplayName: Christian Wach
Answers.Body: <p>Not perfect (see below), but I've made this work in a pinch: </p>
  
  <p>As of civicrm version 5.17 at least, <code>addCoreResources($region)</code> works if you specify <code>'page-footer'</code> for <code>$region</code>.</p>
  
  <pre><code>CRM_Core_Resources::singleton()-&gt;addCoreResources('page-footer');
  </code></pre>
  
  <p>The function signature for <code>addCoreResources()</code> says <code>addCoreResources('html-header')</code>, 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 <code>CRM_Core_Resources::singleton()-&gt;addScriptFile('com.example.extensionname', 'js/example.js', $weight=1, 'html-header')</code> 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.</p>
  
  <p>So by specifying the 'page-footer' region for the core resources, they are included.</p>
  
  <p>Other thoughts:</p>
  
  <ul>
  <li>I'm unsure what other regions may be provide the same effect;</li>
  <li>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.</li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-27T19:24:41.057Z
Answers.LastActivityDate: 2020-02-27T19:24:41.057Z
Answers.UserDisplayName: TwoMice
Body: <p>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. </p>
  
  <p><a href="http://wiki.civicrm.org/confluence/display/CRMDOC/Javascript+Reference#JavascriptReference-UseonNon-CiviCRMPages" rel="noreferrer">http://wiki.civicrm.org/confluence/display/CRMDOC/Javascript+Reference#JavascriptReference-UseonNon-CiviCRMPages</a></p>
  
  <p>In functions.php I've tried this: </p>
  
  <p>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</p>
  
  <p>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.</p>
  
  <p>What I want to do is grab the event list in JSON format. </p>
  
  <p>Any clues? What am I missing?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-03T10:30:54.220Z
Comments.Text: Did you ever solve this? addCoreResources() still doesn't work in WordPress in 4.7, as far as I can tell.
Comments.UserDisplayName: Andrew West
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-03T11:13:59.660Z
Comments.Text: Trying to track this one down. I've added a bug report: https://issues.civicrm.org/jira/browse/CRM-20806
Comments.UserDisplayName: Andrew West
Tags: <wordpress><api>
Title: How do I access the JavaScript API via a WordPress theme template?
ThreadText: # How do I access the JavaScript API via a WordPress theme template?
  
  (Asked by: username_0 on 2015-05-19)
  
  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?
  
  **Tags:** wordpress, api
  
  > **Comments:**
  > **username_1**: Did you ever solve this? addCoreResources() still doesn't work in WordPress in 4.7, as far as I can tell.  
  > **username_1**: Trying to track this one down. I've added a bug report: https://issues.civicrm.org/jira/browse/CRM-20806  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2015-05-19. Score: 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();
  }
  
  ```
  
  
  ---
  
  ### Answer by username_3 on 2020-02-27. Score: 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.
  
  
  ---
  
  