Actions

Difference between revisions of "Microdata"

From Joomla! Documentation

Line 31: Line 31:
 
<br>
 
<br>
 
<big>
 
<big>
<source>
+
<code>
 
JMicrodata::htmlScope($scope);
 
JMicrodata::htmlScope($scope);
</source>
+
</code>
 
</big>  
 
</big>  
 +
<br>
 
Return:
 
Return:
 
<source lang="php">
 
<source lang="php">
Line 43: Line 44:
 
<br>
 
<br>
 
<br>
 
<br>
<big><source lang="php">
+
<big><code>
 
JMicrodata::htmlProperty($property);
 
JMicrodata::htmlProperty($property);
</source></big>  
+
</code></big><br>  
 
Return:
 
Return:
 
<source lang="php">
 
<source lang="php">
Line 54: Line 55:
 
<br>
 
<br>
 
<br>
 
<br>
<big><source lang="php">
+
<big><code>
 
JMicrodata::htmlSpan($content, $property = '', $scope = '', $inverse = false);
 
JMicrodata::htmlSpan($content, $property = '', $scope = '', $inverse = false);
</source></big>  
+
</code></big><br>  
 
Return:
 
Return:
 
<source lang="php">
 
<source lang="php">
Line 66: Line 67:
 
<br>
 
<br>
 
<br>
 
<br>
<br>
+
<br><big>
<source lang="php">
+
<code>
 
JMicrodata::htmlMeta($content, $property, $scope = '', $inverse = false);
 
JMicrodata::htmlMeta($content, $property, $scope = '', $inverse = false);
</source>  
+
</code></big><br>
 
Return:
 
Return:
 
<source lang="php">
 
<source lang="php">
Line 85: Line 86:
 
<br>
 
<br>
 
<br>
 
<br>
<source lang="php">
+
<big><code>
 
$microdata->enable($flag = true);
 
$microdata->enable($flag = true);
</source>  
+
</code></big><br>  
 
Enable or Disable HTML Microdata semantics output.
 
Enable or Disable HTML Microdata semantics output.
 
<br>
 
<br>
 
<br>
 
<br>
<br>
+
<br><big>
<source lang="php">
+
<code>
 
$microdata->isEnabled();
 
$microdata->isEnabled();
</source>  
+
</code></big><br>  
 
Return true if Microdata semantics HTML output are enabled.
 
Return true if Microdata semantics HTML output are enabled.
 
<br>
 
<br>
 
<br>
 
<br>
<br>
+
<br><big>
<source lang="php">
+
<code>
 
$microdata->setType($type);
 
$microdata->setType($type);
</source>  
+
</code></big><br>
 
Set a new Schema.org <i>Type</i>, there is also a $microdata->getType() function to retrieve the current <i>Type</i>.
 
Set a new Schema.org <i>Type</i>, there is also a $microdata->getType() function to retrieve the current <i>Type</i>.
 
<br>
 
<br>
 
<br>
 
<br>
<br>
+
<br><big>
<source lang="php">
+
<code>
 
$microdata->property($name);
 
$microdata->property($name);
</source>  
+
</code></big><br>  
 
Setup the <i>Property</i> if available in the current <i>Type</i> Scope, there is also a $microdata->getProperty() function to retrieve the current <i>Property</i>.
 
Setup the <i>Property</i> if available in the current <i>Type</i> Scope, there is also a $microdata->getProperty() function to retrieve the current <i>Property</i>.
 
<br>
 
<br>
 
<br>
 
<br>
 
<br>
 
<br>
<source lang="php">
+
<big><code>
 
$microdata->content($value, $machineValue = null);
 
$microdata->content($value, $machineValue = null);
</source>  
+
</code></big><br>  
 
Setup a <i>Text value</i> or <i>Content value</i> for the Microdata, there is also a $microdata->getContent() function to retrieve the current <i>Text value</i>.
 
Setup a <i>Text value</i> or <i>Content value</i> for the Microdata, there is also a $microdata->getContent() function to retrieve the current <i>Text value</i>.
 
<br>
 
<br>
 
<br>
 
<br>
 
<br>
 
<br>
<source lang="php">
+
<big><code>
 
$microdata->fallback($type, $property);
 
$microdata->fallback($type, $property);
</source>  
+
</code>  
Setup a Fallback <i>Type</i> and <i>Property</i>, there are also the $microdata->getFallbackType() and $microdata->getFallbackProperty() to retrieve the Fallback <i>Type</i> and Fallback <i>Property</i>. Fallback to <i>Thing Type</i> if the <i>Type</i> isn't available, Fallback to <i>null</i> the Property if isn't available.
+
</big><br>Setup a Fallback <i>Type</i> and <i>Property</i>, there are also the $microdata->getFallbackType() and $microdata->getFallbackProperty() to retrieve the Fallback <i>Type</i> and Fallback <i>Property</i>. Fallback to <i>Thing Type</i> if the <i>Type</i> isn't available, Fallback to <i>null</i> the Property if isn't available.
 
<br>
 
<br>
 
<br>
 
<br>
<br>
+
<br><big>
<source lang="php">
+
<code>
 
$microdata->displayScope();
 
$microdata->displayScope();
</source>
+
</code></big><br>
 
Return:  
 
Return:  
 
<source lang="php">
 
<source lang="php">
Line 137: Line 138:
 
<br>
 
<br>
 
<br>
 
<br>
<br>
+
<br><big>
<source lang="php">
+
<code>
 
$microdata->display($displayType = '', $emptyOutput = false);
 
$microdata->display($displayType = '', $emptyOutput = false);
</source>
+
</code></big><br>
 
Return the Microdata HTML, if the <i>Property</i> isn't available it checks for a Fallback, otherwise return ''.
 
Return the Microdata HTML, if the <i>Property</i> isn't available it checks for a Fallback, otherwise return ''.
 
<br>
 
<br>

Revision as of 12:55, 13 December 2013

Quill icon.png
Content is Incomplete

This article or section is incomplete, which means it may be lacking information. You are welcome to assist in its completion by editing it as well. If this article or section has not been edited in several days, please consider helping complete the content.
This article was last edited by P.Alex (talk| contribs) 11 months ago. (Purge)


Contents

What is Microdata?

Microdata is a way of adding contextual information to your website and its contents, allowing search engines to better understand the information you provide them with.

Contextual information allows search engines to understand the meaning of the information presented on your website, which allows it to better answer more verbose 'natural language' queries where an understanding of the meaning helps to interpret the most relevant content to be displayed.

Microdata can be used to explain just about anything you would ever want to explain, and there are more 'schemas' being added on a regular basis. There are several vocabularies in existence, however at present the system favoured by search engines is that of schema.org.
To understand how search engines use the microdata information take a look at this short video.

How do I use Microdata?

Microdata can be added to Joomla! using template overrides or with the use of plugins which allow you to insert microdata into specific resources.
As of Joomla! Joomla 3.2 there is a library within Joomla! which allows developers to pull in microdata without needing to format it correctly. The following resources are available by the author of the microdata library introducing the library and how to use it:

JMicrodata

JMicrodata is a Joomla 3.2 library to implement http://schema.org microdata semantics.
The JMicrodata class uses the types.json file which contains all available http://schema.org Types and Properties.
The types.json was automatically created with the https://github.com/PAlexcom/Spider4Schema web crawler.

For some usage examples you can see the library test file https://github.com/joomla/joomla-cms/blob/master/tests/unit/suites/libraries/joomla/microdata/JMicrodataTest.php

JMicrodata documentation

All Microdata HTML output is handled by the JMicrodata class.

JMicrodata::htmlScope($scope);
Return:

itemscope itemtype="http://schema.org/$scope"

the HTML Scope of the given Type, must be inside a HTML tag element.


JMicrodata::htmlProperty($property);
Return:

itemprop="$property"

the HTML of the given Property, must be inside a HTML tag element.


JMicrodata::htmlSpan($content, $property = , $scope = , $inverse = false);
Return:

<span itemscope itemtype="http://schema.org/$scope itemprop="$property">
    $content
</span>

the microdata in a tag, there is also available a JMicrodata::htmlDiv() method with the same functionalities.


JMicrodata::htmlMeta($content, $property, $scope = , $inverse = false);
Return:

<meta $property $scope content='$content'>

the microdata in a <meta> tag with content for machines.


$microdata = JMicrodata($type = '', $flag = true);

Create a new instance of the JMicrodata class and setup the current Type, the flag param is for enabling or disabling HTML microdata semantics output. Fallback to Thing Type if the Type isn't available or given.


$microdata->enable($flag = true);
Enable or Disable HTML Microdata semantics output.


$microdata->isEnabled();
Return true if Microdata semantics HTML output are enabled.


$microdata->setType($type);
Set a new Schema.org Type, there is also a $microdata->getType() function to retrieve the current Type.


$microdata->property($name);
Setup the Property if available in the current Type Scope, there is also a $microdata->getProperty() function to retrieve the current Property.


$microdata->content($value, $machineValue = null);
Setup a Text value or Content value for the Microdata, there is also a $microdata->getContent() function to retrieve the current Text value.


$microdata->fallback($type, $property);
Setup a Fallback Type and Property, there are also the $microdata->getFallbackType() and $microdata->getFallbackProperty() to retrieve the Fallback Type and Fallback Property. Fallback to Thing Type if the Type isn't available, Fallback to null the Property if isn't available.


$microdata->displayScope();
Return:

itemscope itemtype="http://schema.org/$scope

the HTML with the Scope of the current Type, must be inserted inside a tag element.


$microdata->display($displayType = , $emptyOutput = false);
Return the Microdata HTML, if the Property isn't available it checks for a Fallback, otherwise return .

There are 4 types of $displayType:

  • inline
  • span
  • html
  • meta

This method contains the HTML Microdata display logic, If you specify the $displayType param, the Microdata will be returned the way you specified and expect — to, Otherwise if the $displayType param is empty it will be processed by the display() method and returned the HTML in the right way, with the expected Property Type (example of expectedTypes = URL, Text, Person ...)

— What happens if you call display($displayType = 'meta') ?
The returned HTML will be inside a <meta> HTML tag.

— What happend if you call display()?
The method will automatically check the expected type for the given Property, and will return the right Microdata HTML.

There are 3 types of Microdata:

  • nested → example:
    itemprop="$property" itemscope itemtype="http://schema.org/$scope
    
    , if there is also available a $content it will output
    <span itemprop="$property" itemscope itemtype="http://schema.org/$scope>$content</span>
    
  • meta → example:
    <meta content="$content" itemprop="$property">
    
    , if no $content is available will output
    itemprop="$property"
    
  • normal → example:
    itemprop="$property"
    
    , if there is available a $content will output
    <span itemprop="$property">$content</span>
    

How do I use the JMicrodata library?

First of all you need to make an instance of the library in your extensions:

$microdata = new JMicrodata('Article');

So let's suppose that you have the following string which is part of your article and the current scope is Article:

echo 'Written by Alexandru Pruteanu';

And the microdata you need to add is an author property:

echo 'Written by' . $microdata->content(“Alexandru Pruteanu”)->property('author')->fallback('Person', 'name')->display();

The library will display:

Written by
<span itemprop='author' itemscope itemtype='https://schema.org/Person'>
    <span itemprop='name'>
        Alexandru Pruteanu
    </span>
</span>

What happens if the current scope is something else than Article, for example a Product scope, and the current scope doesn't have an author property?
Well it will fallback in:

Written by
<span itemscope itemtype='https://schema.org/Person'>
    <span itemprop='name'>
        Alexandru Pruteanu
    </span>
</span>

If I want to disable the microdata semantics output?
You can simply disable the microdata output by calling the following function:

$microdata->enable(false);

The library will display the following:

Written by Alexandru Pruteanu

How To implement Microdata yourself