You are here
How to add matadata keywords in drupal 7.
function abctheme_preprocess_page(&$vars, $hook) {
//get node id inorder to load metadata in the head
if (isset($vars['node'])) {
$abc_nid = $vars['node']->nid;
variable_set('abc_nid', $abc_nid);
}
}
function abc_meta_keywords() {
$abc_node = 'Default meta keywords if nothig else is loaded';
$abc_nid = variable_get('abc_nid', $default = 1);
$abc_node1 = node_load($nid = $abc_nid, $vid = NULL, $reset = FALSE);
dsm($abc_node1->field_metadata_keywords['und'][0]['safe_value']);
if (isset($abc_node1->field_metadata_keywords['und'][0])) {
$abc_node = $abc_node1->field_metadata_keywords['und'][0]['safe_value'];
}
return check_plain($abc_node);
}
/*
* Loading the Matadata description and keywords for the node from database and adding it to head
*/
function abctheme_page_alter($page) {
$meta_keywords = array(
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => array(
'name' => 'keywords',
'content' => abc_meta_keywords()
)
);
drupal_add_html_head($meta_keywords, 'abc_meta_keywords');
}