Skip to content

Instantly share code, notes, and snippets.

@init90
Created May 6, 2019 15:56
Show Gist options
  • Save init90/aafddcdbac76aab4a1ec090d6f2baa23 to your computer and use it in GitHub Desktop.
Save init90/aafddcdbac76aab4a1ec090d6f2baa23 to your computer and use it in GitHub Desktop.
Drupal 7, create metatags programality.
// The type of entity being edited.
$entity_type = 'node';
// The ID of the entity being changed.
$entity_id = 42;
// The revision ID for this entity, leave as NULL to ignore revisioning.
$revision_id = NULL;
// The language code for this entity.
$langcode = LANGUAGE_NONE;
// Load the existing values, will return an empty array if anything found.
$metatags = metatag_metatags_load($entity_type, $entity_id, $revision_id);
// Set the new meta tags. Most meta tags have a 'value' attribute.
$metatags[$langcode]['title']['value'] = 'The title | [site:name]';
$metatags[$langcode]['description']['value'] = 'The description';
// Update the meta tags. This also clears the entity's cache.
metatag_metatags_save($entity_type, $entity_id, $revision_id, $metatags);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment