Actions

Difference between revisions of "Microdata"

From Joomla! Documentation

(How do I use the JMicrodata library?)
(11 intermediate revisions by one user not shown)
Line 13: Line 13:
 
Microdata can be added to Joomla! using template overrides or with the use of plugins which allow you to insert microdata into specific resources.
 
Microdata can be added to Joomla! using template overrides or with the use of plugins which allow you to insert microdata into specific resources.
 
<br>
 
<br>
<u>As of '''Joomla!''' {{JVer|3.2}} there is a [https://github.com/joomla/joomla-cms/tree/master/libraries/joomla/microdata library] within Joomla! which allows developers to pull in microdata without needing to format it correctly</u>. The following resources are available by the author of the microdata library introducing the library and how to use it:
+
As of '''Joomla!''' {{JVer|3.2}} there is a [https://github.com/joomla/joomla-cms/tree/master/libraries/joomla/microdata library] within Joomla! which allows developers to pull in microdata without needing to format it correctly.
*http://magazine.joomla.org/issues/issue-oct-2013/item/1551-what-about-microdata-rich-snippets-in-joomla
+
  
 
==JMicrodata==
 
==JMicrodata==
[https://github.com/joomla/joomla-cms/blob/master/libraries/joomla/microdata/microdata.php JMicrodata] is a {{JVer|3.2}} [https://github.com/joomla/joomla-cms/tree/master/libraries/joomla/microdata library] to implement http://schema.org microdata semantics.
+
[https://github.com/joomla/joomla-cms/blob/master/libraries/joomla/microdata/microdata.php JMicrodata] is a library to implement and output http://schema.org microdata semantics.
 
<br>
 
<br>
The JMicrodata class uses the [https://github.com/joomla/joomla-cms/blob/master/libraries/joomla/microdata/types.json types.json] file which contains all available http://schema.org <i>Types</i> and <i>Properties</i>.
 
 
<br>
 
<br>
The <code>'''types.json'''</code> file was automatically created with the https://github.com/PAlexcom/Spider4Schema web crawler.
+
The library was designed with this goals in mind:
<br>
+
# Having the '''possibility to switch the Microdata Type dynamically''', you just change the Type (there are 558 different available types).
<br>
+
# '''Display validated semantics''', the library takes care to display data correctly.
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
+
# '''Enable/disable the microdata''' semantics.
 +
# '''Fallbacks''', you should never lose any meaningful semantic.
 +
The JMicrodata class uses the [https://github.com/joomla/joomla-cms/blob/master/libraries/joomla/microdata/types.json types.json] file which contains all available http://schema.org <i>Types</i> and <i>Properties</i>, that file was automatically created with the https://github.com/PAlexcom/Spider4Schema web crawler.
 +
 
 +
==How do I use the JMicrodata library?==
 +
First of all you need to make an instance of the library in your extensions:
 +
<source lang="php">
 +
<?php
 +
$microdata = new JMicrodata('Article');
 +
?>
 +
</source>
 +
So let's suppose that you have the following string which is part of your article:
 +
<source lang="html4strict">
 +
<div>
 +
    <!-- Author of the content -->
 +
    <span>
 +
        Written by Alexandru Pruteanu
 +
    </span>
 +
    <!-- The content -->
 +
    Here is the article text...
 +
<div>
 +
</source>
 +
And you want to add microdata semantics and you setup the current scope type as <i>Article</i>:
 +
<source lang="php">
 +
<?php
 +
$microdata = new JMicrodata('Article');
 +
?>
 +
 
 +
<div <?php echo $microdata->displayScope();?>>
 +
    <!-- Author of the content -->
 +
    <span>
 +
        Written by <?php echo $microdata->content('Alexandru Pruteanu')->property('author')->fallback('Person', 'name')->display();?>
 +
    </span>
 +
    <!-- The content -->
 +
    <?php echo $microdata->content('Here is the article text...')->property('articleBody')->display();?>
 +
<div>
 +
</source>
 +
The library will display:
 +
<source lang="html4strict">
 +
<div itemscope itemtype='https://schema.org/Article'>
 +
    <!-- Author of the content -->
 +
    <span>
 +
        Written by
 +
        <span itemprop='author' itemscope itemtype='https://schema.org/Person'>
 +
            <span itemprop='name'>Alexandru Pruteanu</span>
 +
        </span>
 +
    </span>
 +
    <!-- The content -->
 +
    <span itemprop='articleBody'>Here is the article text...</span>
 +
<div>
 +
</source>
 +
What happens if the current scope is something else than <i>Article</i>, for example a <i>Product</i> scope, and the current scope doesn't have an <i>author</i> and <i>articleBody</i> property?
 +
<source lang="php">
 +
<?php
 +
$microdata = new JMicrodata('Product');
 +
?>
 +
 
 +
<div <?php echo $microdata->displayScope();?>>
 +
    <!-- Author of the content -->
 +
    <span>
 +
        Written by <?php echo $microdata->content('Alexandru Pruteanu')->property('author')->fallback('Person', 'name')->display();?>
 +
    </span>
 +
    <!-- The content -->
 +
    <?php echo $microdata->content('Here is the article text...')->property('articleBody')->display();?>
 +
<div>
 +
</source>
 +
As you've added a fallback <tt>->fallback('Person', 'name')</tt>, it will fallback to the <i>Person</i> type, and you will not lose any meaningful semantic.
 +
<source lang="html4strict">
 +
<div itemscope itemtype='https://schema.org/Product'>
 +
    <!-- Author of the content -->
 +
    <span>
 +
        Written by
 +
        <span itemscope itemtype='https://schema.org/Person'>
 +
            <span itemprop='name'>Alexandru Pruteanu</span>
 +
        </span>
 +
    </span>
 +
    <!-- The content -->
 +
    Here is the article text...
 +
<div>
 +
</source>
 +
If you don't need all that microdata information, you just disable that feature by calling: <tt>$microdata->enable(false);</tt>
 +
<source lang="php">
 +
<?php
 +
$microdata = new JMicrodata('Product');
 +
$microdata->enable(false);
 +
?>
 +
 
 +
<div <?php echo $microdata->displayScope();?>>
 +
    <!-- Author of the content -->
 +
    <span>
 +
        Written by <?php echo $microdata->content('Alexandru Pruteanu')->property('author')->fallback('Person', 'name')->display();?>
 +
    </span>
 +
    <!-- The content -->
 +
    <?php echo $microdata->content('Here is the article text...')->property('articleBody')->display();?>
 +
<div>
 +
 
 +
</source>
 +
The library will display the following:
 +
<source lang="html4strict">
 +
<div>
 +
    <!-- Author of the content -->
 +
    <span>
 +
        Written by Alexandru Pruteanu
 +
    </span>
 +
    <!-- The content -->
 +
    Here is the article text...
 +
<div>
 +
</source>
 +
 
 
==JMicrodata documentation==
 
==JMicrodata documentation==
 
All Microdata HTML output is handled by the JMicrodata class.
 
All Microdata HTML output is handled by the JMicrodata class.
Line 145: Line 251:
 
itemprop="$property"</source>, if there is available a $content will output <source lang="php">
 
itemprop="$property"</source>, if there is available a $content will output <source lang="php">
 
<span itemprop="$property">$content</span></source>
 
<span itemprop="$property">$content</span></source>
==How do I use the JMicrodata library?==
 
First of all you need to make an instance of the library in your extensions:
 
<source lang="php">
 
$microdata = new JMicrodata('Article');
 
</source>
 
So let's suppose that you have the following string which is part of your article and the current scope is <i>Article</i>:
 
<source lang="php">
 
echo 'Written by Alexandru Pruteanu';
 
</source>
 
And the microdata you need to add is an <i>author</i> property:
 
<source lang="php">
 
echo 'Written by ' . $microdata->content(“Alexandru Pruteanu”)->property('author')->fallback('Person', 'name')->display();
 
</source>
 
The library will display:
 
<source lang="html4strict">
 
Written by
 
<span itemprop='author' itemscope itemtype='https://schema.org/Person'>
 
    <span itemprop='name'>
 
        Alexandru Pruteanu
 
    </span>
 
</span>
 
</source>
 
What happens if the current scope is something else than <i>Article</i>, for example a <i>Product</i> scope, and the current scope doesn't have an <i>author</i> property?
 
 
<br>
 
<br>
Well it will fallback in:
+
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
<source lang="html4strict">
+
 
Written by
+
<span itemscope itemtype='https://schema.org/Person'>
+
    <span itemprop='name'>
+
        Alexandru Pruteanu
+
    </span>
+
</span>
+
</source>
+
If I want to disable the microdata semantics output?
+
<br>
+
You can simply disable the microdata output by calling the following function:
+
<source lang="php">
+
$microdata->enable(false);
+
</source>
+
The library will display the following:
+
<source lang="html4strict">
+
Written by Alexandru Pruteanu
+
</source>
+
 
==How To implement Microdata yourself==
 
==How To implement Microdata yourself==
 
* [[How To Implement Rich Snippet for Breadcrumbs]]
 
* [[How To Implement Rich Snippet for Breadcrumbs]]
 
[[Category:Search Engine Optimisation]]
 
[[Category:Search Engine Optimisation]]

Revision as of 13:49, 7 April 2014

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) 5 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.

JMicrodata

JMicrodata is a library to implement and output http://schema.org microdata semantics.

The library was designed with this goals in mind:

  1. Having the possibility to switch the Microdata Type dynamically, you just change the Type (there are 558 different available types).
  2. Display validated semantics, the library takes care to display data correctly.
  3. Enable/disable the microdata semantics.
  4. Fallbacks, you should never lose any meaningful semantic.

The JMicrodata class uses the types.json file which contains all available http://schema.org Types and Properties, that file was automatically created with the https://github.com/PAlexcom/Spider4Schema web crawler.

How do I use the JMicrodata library?

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

<?php
$microdata = new JMicrodata('Article');
?>

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

<div>
    <!-- Author of the content -->
    <span>
        Written by Alexandru Pruteanu
    </span>
    <!-- The content -->
    Here is the article text...
<div>

And you want to add microdata semantics and you setup the current scope type as Article:

<?php
$microdata = new JMicrodata('Article');
?>
 
<div <?php echo $microdata->displayScope();?>>
    <!-- Author of the content -->
    <span>
        Written by <?php echo $microdata->content('Alexandru Pruteanu')->property('author')->fallback('Person', 'name')->display();?>
    </span>
    <!-- The content -->
    <?php echo $microdata->content('Here is the article text...')->property('articleBody')->display();?>
<div>

The library will display:

<div itemscope itemtype='https://schema.org/Article'>
    <!-- Author of the content -->
    <span>
        Written by
        <span itemprop='author' itemscope itemtype='https://schema.org/Person'>
            <span itemprop='name'>Alexandru Pruteanu</span>
        </span>
    </span>
    <!-- The content -->
    <span itemprop='articleBody'>Here is the article text...</span>
<div>

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 and articleBody property?

<?php
$microdata = new JMicrodata('Product');
?>
 
<div <?php echo $microdata->displayScope();?>>
    <!-- Author of the content -->
    <span>
        Written by <?php echo $microdata->content('Alexandru Pruteanu')->property('author')->fallback('Person', 'name')->display();?>
    </span>
    <!-- The content -->
    <?php echo $microdata->content('Here is the article text...')->property('articleBody')->display();?>
<div>

As you've added a fallback ->fallback('Person', 'name'), it will fallback to the Person type, and you will not lose any meaningful semantic.

<div itemscope itemtype='https://schema.org/Product'>
    <!-- Author of the content -->
    <span>
        Written by
        <span itemscope itemtype='https://schema.org/Person'>
            <span itemprop='name'>Alexandru Pruteanu</span>
        </span>
    </span>
    <!-- The content -->
    Here is the article text...
<div>

If you don't need all that microdata information, you just disable that feature by calling: $microdata->enable(false);

<?php
$microdata = new JMicrodata('Product');
$microdata->enable(false);
?>
 
<div <?php echo $microdata->displayScope();?>>
    <!-- Author of the content -->
    <span>
        Written by <?php echo $microdata->content('Alexandru Pruteanu')->property('author')->fallback('Person', 'name')->display();?>
    </span>
    <!-- The content -->
    <?php echo $microdata->content('Here is the article text...')->property('articleBody')->display();?>
<div>

The library will display the following:

<div>
    <!-- Author of the content -->
    <span>
        Written by Alexandru Pruteanu
    </span>
    <!-- The content -->
    Here is the article text...
<div>

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 the content for machines, this method does not add the meta tag in the <head> section of the page.


$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>
    


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

How To implement Microdata yourself