Answers.Body: <p>When faced with this situation, I usually examine <code>$form</code> to find it. </p>
  
  <p>For quick and easy lookup, I use <code>CRM_Core_Error::debug</code> to write it to the civi log.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-29T21:29:00.160Z
Answers.Text: I'm not sure this will help me, I've been using smartyDebug to find variables on the webpage. Using smartyDebug I know that the variable $contributionPageID exists on this form/page. I can also verify that the variable I've created ($cPageName) comes through it is just null. What I don't know is how to utilize the existing variable in my hook.
Answers.UserDisplayName: RayWright
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-29T21:37:14.420Z
Answers.Text: I guess I am not sure of your exact problem.  Is `$ctid` getting properly populated from `$form->getVar`?  Is that where the problem is occurring?
Answers.UserDisplayName: DaveD
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-29T21:39:37.123Z
Answers.Text: Right, [$ctid = $form->getVar('_contributionPageID');] doesn't return the data '3' that it should (or I think it should). I tried with and without the underscore.
Answers.UserDisplayName: RayWright
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-29T21:56:28.123Z
Answers.Text: Well, I would probably still try my suggestion on trying `CRM_Core_Error::debug` just in case you can find the correct variable (that perhaps smartydebug doesn't reveal).  Or did you check the smartydebug output for other instances of the variable that may hold the correct value?
Answers.UserDisplayName: DaveD
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-29T20:59:22.383Z
Answers.LastActivityDate: 2015-05-29T20:59:22.383Z
Answers.UserDisplayName: DaveD
Answers.Body: <p>Looks like you are trying to access a variable that's been assigned to the template, not the object. I'd say just access the variable <code>$form-&gt;_id</code> directly. Also, I suggest you do not use raw sql queries to retrieve a CiviCRM entity. Here's an example using the api:</p>
  
  <pre><code>function wordpress_civicrm_buildForm($formName, &amp;$form) {
    $page = civicrm_api3('contribution_page', 'getsingle', array(
      'id' =&gt; $form-&gt;_id,
      'check_permissions' =&gt; FALSE,
      'return' =&gt; 'title',
    ));
    $form-&gt;assign('cPageName', $page['title']);
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-01T18:54:56.383Z
Answers.Text: This does it, thank you for the tip about not using a raw sql query. I will have to work on understanding the api a bit better to be able to write my own.
Answers.UserDisplayName: RayWright
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-01T19:45:06.933Z
Answers.Text: Start with the [API Explorer](https://civicrm.org/blogs/colemanw/meet-new-api-explorer), it's your friend :)
Answers.UserDisplayName: Coleman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-04T14:47:41.683Z
Answers.Text: I should note that I had to add an if statement to make sure it was a front end page. The above code on it's own caused an api error in the backend. $config = CRM_Core_Config::singleton();
   if ( $config->userFrameworkFrontend ) { //cool code here....
Answers.UserDisplayName: RayWright
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-30T00:55:59.923Z
Answers.LastActivityDate: 2015-05-30T00:55:59.923Z
Answers.UserDisplayName: Coleman
Body: <p>So I've almost got a hook working to add the name of the contribution page to the main.tpl part of a PCP.</p>
  
  <p>This is what I have so far: </p>
  
  <pre><code>function wordpress_civicrm_buildForm($formName, &amp;$form) {
  
    if (  $formName == 'CRM_Contribute_Form_Contribution_Main' ) {    
          $ctid = $form-&gt;getVar('_contributionPageID');
  
          $cpagequery = mysql_query("
          SELECT *
          FROM civicrm_contribution_page
          WHERE civicrm_contribution_page.id = $ctid
          ");
  
          while ($cnamerow = mysql_fetch_array($cpagequery)){
                  $cPageName = $cnamerow['title'];
          }
  
          $form-&gt;assign('cPageName', $cPageName);
      }
  }
  </code></pre>
  
  <p>The part that's not working is using the $contributionPageID variable that is already exists on the page. I know the query works, because if I substitute a number for the $contributionPageID I get a result. Any idea how I can pull the contributionPageID variable for use in this hook?</p>
  
Tags: <civicontribute><hooks>
Title: Trying to write a hook to add contribution page name to Main.tpl I'm super close
ThreadText: # Trying to write a hook to add contribution page name to Main.tpl I'm super close
  
  (Asked by: username_0 on 2015-05-29)
  
  So I've almost got a hook working to add the name of the contribution page to the main.tpl part of a PCP.
  This is what I have so far: 
  function wordpress_civicrm_buildForm($formName, &$form) {
  
    if (  $formName == 'CRM_Contribute_Form_Contribution_Main' ) {    
          $ctid = $form->getVar('_contributionPageID');
  
          $cpagequery = mysql_query("
          SELECT *
          FROM civicrm_contribution_page
          WHERE civicrm_contribution_page.id = $ctid
          ");
  
          while ($cnamerow = mysql_fetch_array($cpagequery)){
                  $cPageName = $cnamerow['title'];
          }
  
          $form->assign('cPageName', $cPageName);
      }
  }
  
  The part that's not working is using the $contributionPageID variable that is already exists on the page. I know the query works, because if I substitute a number for the $contributionPageID I get a result. Any idea how I can pull the contributionPageID variable for use in this hook?
  
  **Tags:** civicontribute, hooks
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2015-05-30. Score: 5 | Accepted answer
  
  Looks like you are trying to access a variable that's been assigned to the template, not the object. I'd say just access the variable $form->_id directly. Also, I suggest you do not use raw sql queries to retrieve a CiviCRM entity. Here's an example using the api:
  function wordpress_civicrm_buildForm($formName, &$form) {
    $page = civicrm_api3('contribution_page', 'getsingle', array(
      'id' => $form->_id,
      'check_permissions' => FALSE,
      'return' => 'title',
    ));
    $form->assign('cPageName', $page['title']);
  }
  
  > **Comments:**
  > **username_0**: This does it, thank you for the tip about not using a raw sql query. I will have to work on understanding the api a bit better to be able to write my own.  
  > **username_2**: Start with the [API Explorer](https://civicrm.org/blogs/colemanw/meet-new-api-explorer), it's your friend :)  
  > **username_0**: I should note that I had to add an if statement to make sure it was a front end page. The above code on it's own caused an api error in the backend. $config = CRM_Core_Config::singleton();  if ( $config->userFrameworkFrontend ) { //cool code here....  
  
  ---
  
  ### Answer by username_1 on 2015-05-29. Score: 1
  
  When faced with this situation, I usually examine `$form` to find it.
  
  For quick and easy lookup, I use `CRM_Core_Error::debug` to write it to the civi log.
  
  > **Comments:**
  > **username_0**: I'm not sure this will help me, I've been using smartyDebug to find variables on the webpage. Using smartyDebug I know that the variable $contributionPageID exists on this form/page. I can also verify that the variable I've created ($cPageName) comes through it is just null. What I don't know is how to utilize the existing variable in my hook.  
  > **username_1**: I guess I am not sure of your exact problem.  Is `$ctid` getting properly populated from `$form->getVar`?  Is that where the problem is occurring?  
  > **username_0**: Right, [$ctid = $form->getVar('_contributionPageID');] doesn't return the data '3' that it should (or I think it should). I tried with and without the underscore.  
  > **username_1**: Well, I would probably still try my suggestion on trying `CRM_Core_Error::debug` just in case you can find the correct variable (that perhaps smartydebug doesn't reveal).  Or did you check the smartydebug output for other instances of the variable that may hold the correct value?  
  
  ---
  
  