Trying to write a hook to add contribution page name to Main.tpl I'm super close
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-&gt;_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, &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']);
}