Is there a function for update meta tag title and description of a node programmatically?
I have many nodes in my ecommerce site that would like to upgrade with a custom script where I take these data from an excel file.

Thanks in advance.

Comments

giganget created an issue. See original summary.

DamienMcKenna’s picture

Component: Code » Documentation
Category: Support request » Task

You would do something like this:

// The node to load.
$nid = 42;
$node = node_load($nid);
$node->metatags[LANGUAGE_NONE]['title']['value'] = 'Some title | [site:name]';
$node->metatags[LANGUAGE_NONE]['description']['value'] = 'Some description.';
node_save($node);

That might be slightly differently if the node is set up with translations, in that case you'd want to use the language's code instead of LANGUAGE_NONE.

Alternatively you could deal directly with Metatag's APIs:

// 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);

Lets add that to the docs.

DamienMcKenna’s picture

Version: 7.x-1.20 » 7.x-1.x-dev
giganget’s picture

Thanks a lot, just what I looking for.

drupalz001’s picture

I've a similar situation:

I've a thousands of nodes with custom metatag in certain fields based on user input. Every metatag are different.

Let's say now i would like to change the canonical token from relative url to absolute url from now onwards and to update all the existing nodes.

How should i do a bulk update of all existing nodes without affecting the custom metatag?

Can i use the above method or do you have a better solution for this?

Thank you!

DamienMcKenna’s picture

Meta tags that output a URL for the current page should be using a token to automatically insert the value, they should not be entered manually. So, if you need to change the format of a canonical URL you should change the appropriate default value (i.e. the defaults defined under admin/config/search/metatag) to have the appropriate token.

drupalz001’s picture

sorry, maybe i didn't make myself clear. i am using a token for canonical url.

My point is to update the old nodes with the new canonical url (token).

Case:

Node 1 canonical url generated based on old token, output as www.example.com/node1 (without https)

I now updated the new default token to absolute url.

Node 2 canonical url generated based on new token, output as https://www.example.com/node1

Question: how to update the Node 1 automatically? The metatag Bulk Revert is not suitable here as i have custom metatag(FB image width, height) on Node 1.

i am using 7.x-1.17.

Thank you!

DamienMcKenna’s picture

@drupalz001: Did you manually fill in the token on each node form, or did you enter it in the node (or content type) default configuration?

drupalz001’s picture

@DamienMcKenna

I set it at admin/config/search/metatags global type as token.

And the node type inherits it from global type.

DamienMcKenna’s picture

@drupalz001: In that case you just have to change the value in the defaults again and then all of the existing nodes will automatically use the new value.

drupalz001’s picture

@DamienMcKenna

Hi, i created a new Node Type, and it works as you describe.

BUT, for my certain Node Type, it behave like this:

1) I add a new node page and i can see the metatag (on node/%/edit form) is showing "using defaults"

2) I go to admin/config/search/metatags global type and edit the Canonical URL from url-A to url-B.

3) The node i created on 1 (and all the nodes which are Using Defaults metatag), the metatag get updated by itself with canonical URL showing "url-A" (not url-B), and it apparently become custom metatag for the node (no more showing "Using Defaults" on node edit form, it showed the canonical url = url-A).

Any ideas?

Thanks for your help!

itsnadeem’s picture

Please see this article to add dynamic meta tags

premium-ben’s picture

@itsnadeem that's irrelevant to the thread and it's also for D8, wheras this is about D7.