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?

2 Answers 2


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']);
  • 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.
    – RayWright
    Jun 1, 2015 at 18:54
  • Start with the API Explorer, it's your friend :)
    – Coleman
    Jun 1, 2015 at 19:45
  • 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....
    – RayWright
    Jun 4, 2015 at 14:47

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.

  • 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.
    – RayWright
    May 29, 2015 at 21:29
  • 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?
    – DaveD
    May 29, 2015 at 21:37
  • 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.
    – RayWright
    May 29, 2015 at 21:39
  • 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?
    – DaveD
    May 29, 2015 at 21:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.