Answers.Body: <p>You can do something like below
  you can use API4 to get the exact machine names</p>
  <p>civicrm/api4#/explorer/CustomField/get?select=%5B&quot;id&quot;%5D&amp;where=%5B%5B&quot;name&quot;,&quot;%3D&quot;,&quot;&quot;%5D,%5B&quot;custom_group_id:name&quot;,&quot;%3D&quot;,&quot;&quot;%5D%5D&amp;limit=0&amp;checkPermissions=0&amp;_index=&quot;0&quot;</p>
  <pre><code>function extname_civicrm_buildForm($formName, &amp;$form) {
    if ($formName == 'CRM_Contribute_Form_Contribution_Main') {
      if (isset($_GET['custom_59'])) {
        $customFieldId = \Civi\Api4\CustomField::get(FALSE)
          -&gt;addSelect('id')
          -&gt;addWhere('name', '=', 'is_eligible')
          -&gt;addWhere('custom_group_id:name', '=', 'gift_aid')
          -&gt;execute()
          -&gt;first()['id'] ?? NULL;
        if (!empty($customFieldId)) {
          $form-&gt;setDefaults([
            'custom_' . $customFieldId =&gt; $_GET['custom_59'],
          ]);
        }
      }
    }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-03-21T17:55:02.553Z
Answers.Text: Thanks @PradeepNayak. However 59 is the custom field ID, right? My local env database is different from the prod database, so I need to get this custom field without using its ID. It's possible?
Answers.UserDisplayName: marcelo2605
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-03-21T17:57:47.827Z
Answers.Text: If you are using the same machine name then yes possible.
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-03-21T17:50:28.480Z
Answers.LastActivityDate: 2024-03-21T18:00:53.970Z
Answers.LastEditDate: 2024-03-21T18:00:53.970Z
Answers.UserDisplayName: Pradeep Nayak
Body: <p>I know I can do it using <code>civicrm_buildForm</code> but I don't know:</p>
  <ul>
  <li>How to get the custom field</li>
  <li>How to set the value to this field</li>
  </ul>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-03-21T17:44:47.320Z
Comments.Text: What is your civicrm version?
Comments.UserDisplayName: Pradeep Nayak
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-03-21T17:45:27.963Z
Comments.Text: @PradeepNayak 5.65.1
Comments.UserDisplayName: marcelo2605
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-03-21T17:51:19.793Z
Comments.Text: Are you talking about offline contribution form?
Comments.UserDisplayName: Pradeep Nayak
Tags: <wordpress><contribution-page><hooks>
Title: Populate a Contribution form custom field from a URL parameter
ThreadText: # Populate a Contribution form custom field from a URL parameter
  
  (Asked by: username_0 on 2024-03-21)
  
  I know I can do it using `civicrm_buildForm` but I don't know:
  
  * How to get the custom field
  * How to set the value to this field
  
  **Tags:** wordpress, contribution-page, hooks
  
  > **Comments:**
  > **username_1**: What is your civicrm version?  
  > **username_0**: @PradeepNayak 5.65.1  
  > **username_1**: Are you talking about offline contribution form?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-03-21. Score: 2 | Accepted answer
  
  You can do something like below
  you can use API4 to get the exact machine names
  civicrm/api4#/explorer/CustomField/get?select=%5B"id"%5D&where=%5B%5B"name","%3D",""%5D,%5B"custom_group_id:name","%3D",""%5D%5D&limit=0&checkPermissions=0&_index="0"
  function extname_civicrm_buildForm($formName, &$form) {
    if ($formName == 'CRM_Contribute_Form_Contribution_Main') {
      if (isset($_GET['custom_59'])) {
        $customFieldId = \Civi\Api4\CustomField::get(FALSE)
          ->addSelect('id')
          ->addWhere('name', '=', 'is_eligible')
          ->addWhere('custom_group_id:name', '=', 'gift_aid')
          ->execute()
          ->first()['id'] ?? NULL;
        if (!empty($customFieldId)) {
          $form->setDefaults([
            'custom_' . $customFieldId => $_GET['custom_59'],
          ]);
        }
      }
    }
  }
  
  > **Comments:**
  > **username_0**: Thanks @PradeepNayak. However 59 is the custom field ID, right? My local env database is different from the prod database, so I need to get this custom field without using its ID. It's possible?  
  > **username_1**: If you are using the same machine name then yes possible.  
  
  ---
  
  