Coder Social home page Coder Social logo

pedroborges / meta-tags Goto Github PK

View Code? Open in Web Editor NEW
42.0 6.0 14.0 22 KB

HTML meta tags generator for PHP. Supports Open Graph, Twitter Cards, and JSON Linked Data out of the box

PHP 100.00%
php meta-tags seo open-graph twitter-cards head json-ld schema

meta-tags's Introduction

PHP Meta Tags Release Issues

HTML meta tags generator for PHP. Supports Open Graph, Twitter Cards, and JSON Linked Data out of the box.

Installation

composer require pedroborges/meta-tags

Basic Usage

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:

  1. <title>
  2. <meta> (General)
  3. <meta property="og:*"> (Open Graph)
  4. <meta name="twitter:*"> (Twitter Cards)
  5. <link>
  6. <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']);

Change Log

All notable changes to this project will be documented at: https://github.com/pedroborges/meta-tags/blob/master/changelog.md

License

Meta Tags is open-sourced software licensed under the MIT license.

Copyright © 2018 Pedro Borges [email protected]

meta-tags's People

Contributors

pedroborges avatar por avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

meta-tags's Issues

mistake in example json-ld

You example:

$tags->jsonld([
    '@context' => 'http://schema.org',
    '@type': 'Person',
    'name': 'Pedro Borges'
]);

must be:

$tags->jsonld([
    '@context' => 'http://schema.org',
    '@type'   => 'Person',
    'name'  =>  'Pedro Borges'
]);

thanks for you class

charset function

I think you should add this function as well

public function charset($set = "utf-8")
    {
        $tag = $this->createTag('meta', ['charset' => $set]);
        $this->addToTagsGroup('meta', 'charset', $tag);
        return $tag;
    }

Wrong ordering of multiple og:images with additional metadata

Hello,

Your lib fails to keep the proper tags ordering when adding og:image, for example:

<meta property="og:image" content="http://image1">
<meta property="og:image" content="http://image2">
<meta property="og:image:type" content="image/png">
<meta property="og:image:width" content="5">
<meta property="og:image:height" content="10">
<meta property="og:image:alt" content="caption">

I've added a failing test in #6

Added Meta-Tag with no value fails in W3C validation

If a Meta tag with no value is added it outputs an incorrect Meta-Tag.
To prevent this it would be the best to add an if condition (check if value is present or not) before the output of the tag.

For example an empty Twitter image tag outputs the following:

<meta name="twitter:image">

This is not correct. To easy solve this problem it would be the best to check if the image value is present. If yes then output this meta tag with the present value, if not do not output this meta tag.

This is extremly useful if the values are generated dynamically. Otherwise it is always necessary to check it directly in the template.

Best regards

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.