and like us on FB

For a number of article we have scheduled, we’re required to obtain meta data from the head of a HTML document. The object graph tags, in particular, are used by the likes of Twitter and Facebook to render the preview text and image associated with links that are shared. The code on this page essentially provides the same functionality by retrieving all the name and property tags, and then returns them in an array.

Once the data is obtained it’s important to cache the result in some way to avoid making repeated, lengthy, and unnecessary requests to the destination website (which we do with Simple Cache). The cache should have a reasonable expiry time to cater for changing head content.

The Result

To return an array of all name and property tags of our sadly neglected Flight website, use the following:

To return a specific name or property value, use the following:

Returned array will look something like this.

The second example function prints the og:image featured image URL string.

The PHP Code

If you’re going to try this out, it’s best to download it to avoid copy errors. Usage requires Simple Cache.

Considerations

  • PHP provides a default get_meta_tags() function, but it provides limited results.

This get_meta_tags() example:

Returns:

  • Google, Facebook, and others, tend to be shying away from OG tags for the purpose of a title and description. This is because once a post garnishes likes, a title change can associate an individuals with content that doesn’t reflect their initial action.
  • Returning a specific value was an afterthought and lacks any kind of error checking. We wrote this function to return an array, and that’s it’s primary purpose.
  • We use an extended version of this code to build preview pages for our truncating services such as shor.tt and fat.ly.
  • Some data you might want from a website isn’t included in meta tags. For example, the title. To extract the title from a HTML page, the following function applies.

  • When we use the primary function, we extract the title tag and add it to the resulting meta array.

Download

The WordPress plugin (requiring the Simple Cache plugin) is to support a number of other plugins. By itself it does very little.

Title: Object Graph and Meta Tags
Description: Retrieve Object Graph and Meta Tags From a Website With PHP. WP plugin supports some of our other plugins.
Download: WP Plugin (V0.1) | PHP Code (V0.2) | Plugin Page

Shortt URL for this post: http://shor.tt/10c1