Answers.Body: <p>Try prefixing your API call with:</p>
  
  <pre><code>civicrm_initialize();
  </code></pre>
  
  <p>The "class.api.php" is for when you want to use the REST API from an external site.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-14T07:41:31.150Z
Answers.Text: class.api.php works as well locally, but it does implement a class that is a wrapper around civicrm_api3, not that function, so doesn't solve your problem indeed.
Answers.UserDisplayName: Xavier
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-14T13:30:39.737Z
Answers.Text: @bgm Not seems to work in the plugin files. I added `civicrm_initialize();` inside the `try {}` just over the api call and still brakes the site. But works well in themes files...
Answers.UserDisplayName: Xavier Moya
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-13T22:37:56.050Z
Answers.LastActivityDate: 2016-01-13T22:37:56.050Z
Answers.UserDisplayName: bgm
Answers.Body: <p>Solved!!!</p>
  
  <pre><code>   function get_contribution_info($f_type){
  
     $params = array(
         'version'    =&gt;  3,
         'financial_type_id' =&gt; $f_type,
         'is_active' =&gt; '1'
     );
     try{
         if ( ! function_exists( 'civicrm_initialize' ) ) { return; }
         civicrm_initialize();
         $result = civicrm_api3('contribution_page', 'get', $params);
     } catch (CiviCRM_API3_Exception $e) {
         // handle error here
         $errorMessage = $e-&gt;getMessage();
         $errorCode = $e-&gt;getErrorCode();
         $errorData = $e-&gt;getExtraParams();
         return array('error' =&gt; $errorMessage, 'error_code' =&gt; $errorCode, 'error_data' =&gt; $errorData);
      }
     return $result[values];
  }
  </code></pre>
  
  <p><strong>The key was on adding <code>civicrm_initialize();</code></strong>. Thanks to <em>@bgm</em> for the advice!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-15T00:18:29.490Z
Answers.LastActivityDate: 2016-01-15T00:18:29.490Z
Answers.UserDisplayName: Xavier Moya
Body: <p>I'm developing a little wordpress plugin which creates a custom post type with a custom field where I <strong>would like to store an ID of one of the active contribution page of CiviCRM</strong> in order to associate this content with the contribution page information.</p>
  
  <p>This very same code works in theme files but fails when is used in plugin files, breaking the site.</p>
  
  <pre><code>   function get_contribution_info($f_type){
     $params = array(
         'version'    =&gt;  3,
          'financial_type_id' =&gt; $f_type,
          'is_active' =&gt; '1'
      );
      try{
         $result = civicrm_api3('contribution_page', 'get', $params);
      } catch (CiviCRM_API3_Exception $e) {
          // handle error here
          $errorMessage = $e-&gt;getMessage();
          $errorCode = $e-&gt;getErrorCode();
          $errorData = $e-&gt;getExtraParams();
          return array('error' =&gt; $errorMessage, 'error_code' =&gt; $errorCode, 'error_data' =&gt; $errorData);
      }
      return $result[values];
      }
  
      $get_contrib = get_contribution_data( '1' );
      $contributions = array();
      $i = 0;
      foreach($get_contrib as $c) {
          $contributions[$i]['label'] = $c['title'];
          $contributions[$i]['value'] = $c['id'];
          $i++;
      }
  </code></pre>
  
  <p>I've tried to add a <code>require_once</code> the <code>class.api.php</code> without positive results.</p>
  
  <p>Thanks in advance for your help!</p>
  
Tags: <civicontribute><wordpress><api>
Title: How to access the APIv3 from wordpress plugin files
ThreadText: # How to access the APIv3 from wordpress plugin files
  
  (Asked by: username_0 on 2016-01-13)
  
  I'm developing a little wordpress plugin which creates a custom post type with a custom field where I **would like to store an ID of one of the active contribution page of CiviCRM** in order to associate this content with the contribution page information.
  
  This very same code works in theme files but fails when is used in plugin files, breaking the site.
  
  ```
     function get_contribution_info($f_type){
     $params = array(
         'version'    =>  3,
          'financial_type_id' => $f_type,
          'is_active' => '1'
      );
      try{
         $result = civicrm_api3('contribution_page', 'get', $params);
      } catch (CiviCRM_API3_Exception $e) {
          // handle error here
          $errorMessage = $e->getMessage();
          $errorCode = $e->getErrorCode();
          $errorData = $e->getExtraParams();
          return array('error' => $errorMessage, 'error_code' => $errorCode, 'error_data' => $errorData);
      }
      return $result[values];
      }
  
      $get_contrib = get_contribution_data( '1' );
      $contributions = array();
      $i = 0;
      foreach($get_contrib as $c) {
          $contributions[$i]['label'] = $c['title'];
          $contributions[$i]['value'] = $c['id'];
          $i++;
      }
  
  ```
  
  I've tried to add a `require_once` the `class.api.php` without positive results.
  
  Thanks in advance for your help!
  
  **Tags:** civicontribute, wordpress, api
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2016-01-13. Score: 4
  
  Try prefixing your API call with:
  
  ```
  civicrm_initialize();
  
  ```
  
  The "class.api.php" is for when you want to use the REST API from an external site.
  
  > **Comments:**
  > **username_2**: class.api.php works as well locally, but it does implement a class that is a wrapper around civicrm_api3, not that function, so doesn't solve your problem indeed.  
  > **username_0**: @username_1 Not seems to work in the plugin files. I added `civicrm_initialize();` inside the `try {}` just over the api call and still brakes the site. But works well in themes files...  
  
  ---
  
  ### Answer by username_0 on 2016-01-15. Score: 2
  
  Solved!!!
  
  ```
     function get_contribution_info($f_type){
  
     $params = array(
         'version'    =>  3,
         'financial_type_id' => $f_type,
         'is_active' => '1'
     );
     try{
         if ( ! function_exists( 'civicrm_initialize' ) ) { return; }
         civicrm_initialize();
         $result = civicrm_api3('contribution_page', 'get', $params);
     } catch (CiviCRM_API3_Exception $e) {
         // handle error here
         $errorMessage = $e->getMessage();
         $errorCode = $e->getErrorCode();
         $errorData = $e->getExtraParams();
         return array('error' => $errorMessage, 'error_code' => $errorCode, 'error_data' => $errorData);
      }
     return $result[values];
  }
  
  ```
  
  **The key was on adding `civicrm_initialize();`**. Thanks to *@username_1* for the advice!
  
  
  ---
  
  