Answers.Body: <p>The code doesn't go in functions.php, you need to create a little extension. See <a href="https://docs.civicrm.org/dev/en/latest/extensions" rel="nofollow noreferrer">https://docs.civicrm.org/dev/en/latest/extensions</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-16T00:43:25.443Z
Answers.LastActivityDate: 2019-01-16T00:43:25.443Z
Answers.UserDisplayName: Demerit
Body: <p>I'm trying to implement a hook for inserting Event custom fields into Scheduled reminders; however, I'm not a developer so I'm really not sure what the code needs to look like. The custom field is custom_115 (Learning Materials link), which I know is the correct field as I located it using the API Explorer. The problem for me has been the hooks; not really understanding the structure. I'm running CiviCRM 5.7.2 on WordPress. I've pasted the following code (which I pieced together from various sources, and partly guessed) into my functions.php file but I know it's wrong and I don't know how to get it to work:</p>
  
  <pre><code>&lt;?php 
  /* CiviCRM token addition for custom_115 */
  function civicrm_tokens(&amp;$tokens) {
    $tokens['event'] = array(
      'event.custom_115' =&gt; 'Custom 115',
      );
  }
  
  
  /**
  * Populates a token named "custom_115" with the value from a custom field 
  * (id 115) for use in Scheduled Reminders
  * Implements hook_civicrm_tokenValues().
  */
  
  function civicrm_tokenValues(&amp;$values, $cids, $job = null, $tokens = array(), $context = null) {
  static $events = array();
  
    if (is_numeric($cids)) {
      // We're being called by CRM/Core/BAO/ActionSchedule.php:465
      // Params are structured differently than they're supposed to be, but we 
      // can work with it. See https://civicrm.stackexchange.com/questions/3573/can-tokenvalue-hooks-be-used-in-scheduled-reminder-emails
  
  if ($event_id = CRM_Utils_Array::value('event.event_id', $values)) {
    // We've been passed an event ID
  
    if (!array_key_exists($event_id, $events)) {
      // We haven't looked up the custom field value yet
  
      // Fetch the custom field value from the database
      $event = civicrm_api3('Event', 'getsingle', array(
        'sequential' =&gt; 1,
        'return' =&gt; "custom_115",
        'id' =&gt; $event_id,
      ));
  
    $val = CRM_Utils_Array::value('custom_115', $event);
  
    // Keep this value in memory to use with the other event participants
    $events[$event_id] = $val;
  
    } else {
      // We stored the value previously
      $val = $events[$event_id];
    }
  
    $values['event.custom_115'] = $val;
    }
  
   }
  }
  ?&gt;
  </code></pre>
  
  <p>I'm assuming that if it is set up properly, I'll be able to locate "Custom_115" as an option in the Tokens list - is that right? Any help/advice would be appreciated. Thanks</p>
  
Tags: <wordpress><civievent><custom-fields><hooks>
Title: Implementing tokens and tokenValues hooks for Event custom field
ThreadText: # Implementing tokens and tokenValues hooks for Event custom field
  
  (Asked by: username_0 on 2018-12-11)
  
  I'm trying to implement a hook for inserting Event custom fields into Scheduled reminders; however, I'm not a developer so I'm really not sure what the code needs to look like. The custom field is custom\_115 (Learning Materials link), which I know is the correct field as I located it using the API Explorer. The problem for me has been the hooks; not really understanding the structure. I'm running CiviCRM 5.7.2 on WordPress. I've pasted the following code (which I pieced together from various sources, and partly guessed) into my functions.php file but I know it's wrong and I don't know how to get it to work:
  
  ```
  <?php 
  /* CiviCRM token addition for custom_115 */
  function civicrm_tokens(&$tokens) {
    $tokens['event'] = array(
      'event.custom_115' => 'Custom 115',
      );
  }
  
  /**
  * Populates a token named "custom_115" with the value from a custom field 
  * (id 115) for use in Scheduled Reminders
  * Implements hook_civicrm_tokenValues().
  */
  
  function civicrm_tokenValues(&$values, $cids, $job = null, $tokens = array(), $context = null) {
  static $events = array();
  
    if (is_numeric($cids)) {
      // We're being called by CRM/Core/BAO/ActionSchedule.php:465
      // Params are structured differently than they're supposed to be, but we 
      // can work with it. See https://civicrm.stackexchange.com/questions/3573/can-tokenvalue-hooks-be-used-in-scheduled-reminder-emails
  
  if ($event_id = CRM_Utils_Array::value('event.event_id', $values)) {
    // We've been passed an event ID
  
    if (!array_key_exists($event_id, $events)) {
      // We haven't looked up the custom field value yet
  
      // Fetch the custom field value from the database
      $event = civicrm_api3('Event', 'getsingle', array(
        'sequential' => 1,
        'return' => "custom_115",
        'id' => $event_id,
      ));
  
    $val = CRM_Utils_Array::value('custom_115', $event);
  
    // Keep this value in memory to use with the other event participants
    $events[$event_id] = $val;
  
    } else {
      // We stored the value previously
      $val = $events[$event_id];
    }
  
    $values['event.custom_115'] = $val;
    }
  
   }
  }
  ?>
  
  ```
  
  I'm assuming that if it is set up properly, I'll be able to locate "Custom\_115" as an option in the Tokens list - is that right? Any help/advice would be appreciated. Thanks
  
  **Tags:** wordpress, civievent, custom-fields, hooks
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-01-16. Score: 0
  
  The code doesn't go in functions.php, you need to create a little extension. See <https://docs.civicrm.org/dev/en/latest/extensions>.
  
  
  ---
  
  