HTML meta tags generator for PHP. Supports Open Graph, Twitter Cards, and JSON Linked Data out of the box
HTML meta tags generator for PHP. Supports Open Graph, Twitter Cards, and JSON Linked Data out of the box.
composer require pedroborges/meta-tags
Create a new MetaTags
instance then you are ready to start adding meta tags:
use PedroBorges\MetaTags\MetaTags;
$tags = new MetaTags;
// <title>My Awesome Site</title>
$tags->title('My Awesome Site');
// <meta name="description" content="My site description">
$tags->meta('description', 'My site description');
// <link rel="canonical" href="https://pedroborg.es">
// <link rel="alternate" hreflang="en" href="https://en.pedroborg.es">
$tags->link('canonical', 'https://pedroborg.es');
$tags->link('alternate', [
'hreflang' => 'en',
'href' => 'https://en.pedroborg.es'
]);
// <meta property="og:title" content="The Title">
// <meta property="og:type" content="website">
// <meta property="og:url" content="https://pedroborg.es">
// <meta property="og:image" content="https://pedroborg.es/cover.jpg">
$tags->og('title', 'The title');
$tags->og('type', 'website');
$tags->og('url', 'https://pedroborg.es');
$tags->og('image', 'https://pedroborg.es/cover.jpg');
// <meta name="twitter:card" content="summary">
// <meta name="twitter:site" content="@pedroborg_es">
$tags->twitter('card', 'summary');
$tags->twitter('site', '@pedroborg_es');
// <script type="application/ld+json">
// {
// "@context": "http://schema.org",
// "@type": "Person",
// "name": "Pedro Borges"
// }
// </script>
$tags->jsonld([
'@context' => 'http://schema.org',
'@type': 'Person',
'name': 'Pedro Borges'
]);
When you are ready to output them, use the render
method inside your template <head>
element:
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<?php echo $tags->render() ?>
You can also render only one tag:
<?php echo $tags->render('title') ?>
Or specify which tags to render:
<?php echo $tags->render(['og', 'twitter', 'json-ld']) ?>
By default Meta Tag will indent the tags with 4 spaces and use the following order:
<title>
<meta>
(General)<meta property="og:*">
(Open Graph)<meta name="twitter:*">
(Twitter Cards)<link>
<script type="application/ld+json">
(JSON-LD)You can change that when instantiating the MetaTag
class:
use PedroBorges\MetaTags\MetaTags;
$tags = new MetaTags("\t", ['meta', 'title', 'link', 'og', 'twitter', 'json-ld']);
All notable changes to this project will be documented at: https://github.com/pedroborges/meta-tags/blob/master/changelog.md
Meta Tags is open-sourced software licensed under the MIT license.
Copyright © 2018 Pedro Borges [email protected]