facebookarchive / facebook-instant-articles-sdk-php

The Facebook Instant Articles SDK for PHP provides a native interface for creating and publishing Instant Articles.
https://instantarticles.fb.com/
Other
232 stars 148 forks source link

Not possible to set multiple meta properties with the same property_name using addMetaProperty method of the InstantArticle class #288

Closed notallow closed 6 years ago

notallow commented 6 years ago

Steps required to reproduce the problem

  1. I want to set multiple <meta property="article:tag" in the IA, e.g.:
    <meta property="article:tag" content="tag1" />
    <meta property="article:tag" content="tag2" />
    <meta property="article:tag" content="tagN" />
  2. I call addMetaProperty repeatedly with the same property_name= "article:tag" but with different tags, e.g. code used in the functions.php of Wordpress theme

    add_filter( 'instant_articles_transformed_element','m_add_article_tag');
    
    function m_add_article_tag ($instant_article) { 
    $post_id = $GLOBALS['_POST']['post_ID'];
    if($tags = get_the_tags($post_id)) {
          foreach($tags as $tag) {
                 $instant_article->addMetaProperty('article:tag', $tag->name);
           }
    }
    
    return $instant_article; 
    }

    Expected Result

All "article:tag" meta properties are set in the IA

Actual Result

Only the last "article:tag" meta property set in the IA

Looks like the problem is metaProperties field declared as array which could not contain multiple elements with the same property_name.

https://github.com/facebook/facebook-instant-articles-sdk-php/blob/5235408c56daaf3aafbfd52e9d9204cc52f14224/src/Facebook/InstantArticles/Elements/InstantArticle.php#L42

everton-rosario commented 6 years ago

Do you have a specific use case which you are trying to add those duplicate tag names? What exactly are you adding to these meta-tags?

notallow commented 6 years ago

I want to implement this rule:

Instant Articles – when adding an Instant Article to your RSS feed, add tags straight to your stories.

from https://www.facebook.com/facebookmedia/get-started/audience-optimization

everton-rosario commented 6 years ago

This is referring to add the tag to the post you are making on Facebook, just like any regular post, by enabling this feature to your page, then using this feature once you are sharing a post.

As far as I know we don't have this configuration granularity to the Instant Article markup document. Do you have any other link to the specs you are following regarding the adding tags to the head of Instant Articles?

notallow commented 6 years ago

The documentation describing interest tags is not very detailed, but Audience Optimization guide mentioned above is referring to Instant Article RSS feed, so this markup should be applied to the Instant Articles.

Also regarding https://developers.facebook.com/docs/instant-articles/reference/article

At the article level, a series of configuration options can be defined in the markup to customize the way the article is presented ... For more detail, refer to our guide to Open Graph markup.

Open Graph markup of the Article contains article:tag property: https://developers.facebook.com/docs/reference/opengraph/object-type/article/

Also I have tested Instant Article manual editor and it accepts multiple meta with property="article:tag" without any warning.

everton-rosario commented 6 years ago

All the Open Graph rules does apply to the Instant Article links, since an Instant Article enabled link sometimes is not rendered as Instant Article (in a desktop browser for example). This way, if the documentation for web links on the audience targeting refers to adding those meta-tags, those were expected to be added into your web article link (the html code on your site only), not inside the Instant Articles markup.

So this will not make sense to implement inside Instant Articles markup/SDK. If this changes in the future, for sure we will want to cover this use case into the SDK.

Thanks for bringing this case.