An helper package to build metadata tags on PHP pages

Maintainers

Package info

github.com/straylightagency/php-metadata

pkg:composer/straylightagency/metadata

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

dev-main 2026-05-14 17:05 UTC

This package is auto-updated.

Last update: 2026-05-14 17:08:06 UTC


README

A helper package to build metadata tags on PHP pages.

Installation

Require this package with composer.

composer require straylightagency/metadata

Laravel without auto-discovery:

If you don't use auto-discovery, add the ServiceProvider to the providers array in config/app.php:

Straylightagency\Metadata\Laravel\MetadataServiceProvider::class,

Then add this line to your facades in config/app.php:

'Metadata' => Straylightagency\Metadata\Laravel\Metadata::class,

Usage

Without Laravel

Instantiate a new MetadataManager object and simply use methods provided by.

$metadata = new \Straylightagency\Metadata\MetadataManager;

$metadata->title( 'Page Title' );
$metadata->description( 'Website description' );
$metadata->url( 'https://my-website-url.com/my_page/' );
$metadata->author( 'John Doe' );
$metadata->twitterCard( 'app' );
$metadata->twitterImage( asset( '/img/twitter_meta.jpg' ) );
$metadata->ogImage( asset( '/img/facebook_meta.jpg' ), width: 1200, height: 600, type: 'jpeg' );
$metadata->ogType( 'website' );

echo $metadata; // print the meta tags

With Laravel

The package provides by default a Facade for Laravel application. You can call methods directly using the Facade or use the alias instead.

use Straylightagency\Metadata\Laravel\Metadata;

Metadata::title( 'Page title' );

API documentation

Examples bellow are using Laravel Facade Metadata.

Set title

Metadata::title( 'My page title', [flags: int = Metadata::ALL] );
Metadata::metaTitle( 'My page title' ); // only a simple meta title
Metadata::twitterTitle( 'My page title' ); // meta title for Twitter Card
Metadata::ogTitle( 'My page title' ); // meta title for OpenGraph

Set description

Metadata::description( 'My page description', [flags: int = Metadata::ALL] );
Metadata::metaDescription( 'My page description' ); // simply a meta description
Metadata::twitterDescription( 'My page description' ); // meta description for Twitter Card
Metadata::ogDescription( 'My page description' ); // meta description for OpenGraph

Set images

You can set many images

// Meta image for both Twitter Card and OpenGraph
Metadata::image(
    url: 'https://my-website-url.com/metacard.jpg', 
    [secure_url: ?string = null], 
    [type: ?string = null], 
    [width: ?int = null], 
    [height: ?int = null], 
    [alt: ?string = null], 
    [flags: int = Metadata::ALL] 
);

// Twitter Card meta image
Metadata::twitterImage(
    url: 'https://my-website-url.com/metacard.jpg', [alt: ?string = null]
);

// Opengraph meta image
Metadata::ogImage(
    url: 'https://my-website-url.com/metacard.jpg', 
    [secure_url: ?string = null], 
    [type: ?string = null], 
    [width: ?int = null], 
    [height: ?int = null], 
    [alt: ?string = null]
);

Set videos for OpenGraph

Metadata::ogVideo(
    url: 'https://my-website-url.com/video.mp4', 
    [secure_url: ?string = null], 
    [type: ?string = null], 
    [width: ?string = null], 
    [height: ?string = null]
);

Set audios for OpenGraph

Metadata::ogAudio(
    url: 'https://my-website-url.com/audio.mp3', 
    [secure_url: ?string = null], 
    [type: ?string = null]
);

Set url

Metadata::url( 'https://my-website-url.com/', [flags: int = Metadata::ALL] ); // meta URL for both Twitter and OpenGraph
Metadata::twitterUrl( 'https://my-website-url.com/' ); // meta URL for Twitter Card
Metadata::ogUrl( 'https://my-website-url.com/' ); // meta URL for OpenGraph

Set author

Metadata::author( 'John Doe' );

Set OpenGraph Type

Metadata::ogType( 'website', [options: array = []]);

Set Twitter Card Type

Metadata::twitterCard( 'summary' );

Set Twitter Site ID

Metadata::twitterSite( '@my_website_twitter_id' );

Set Twitter Creator ID

Metadata::twitterSite( '@creator_id' );

Set Facebook App ID

Metadata::fbAppId( 'facebook_id' );

Set Facebook Admins ID

Metadata::fbAdmins( 'facebook_admins' );

Set OpenGraph Site Name

Metadata::ogSiteName( 'My website name' );

Set OpenGraph Locale

Metadata::ogLocale( 'en_US' );
Metadata::ogLocaleAlternate( 'fr_FR', 'nl_BE', 'de_DE' ); // set alternates locales available for this page

// or you can combine both using second parameter of ogLocale : 
Metadata::ogLocale( 'en_US', 'fr_FR', 'nl_BE', 'de_DE' );

Set Google Site Verification ID

Metadata::googleSiteVerification( 'google_site_verification_id_from_google_cloud_console' );

Set Google Bot and Google Bot News

Metadata::googleBot( 'notranslate' );
Metadata::googleBotNews( 'nosnippet' );

Set Robots meta

Metadata::robots( 'nofollow', 'noindex' );
Metadata::robots( [ 'nofollow', 'noindex' ] ); // works too

Disable Pinterest Rich Pin

Metadata::disablePinterestRichPin([value: bool = true]);

Set website content rating

Metadata::rating( 'adult' );

Set a simple meta value

Metadata::meta( 'name', 'some meta value' );

Set a Twitter Card value

Metadata::twitter( 'name', 'some Twitter Card value', [prefix: MetadataManager::TWITTER_PREFIX], [uniq: bool = false]);

Set an OpenGraph value

Metadata::opengraph( 'name', 'some OpenGraph value', [prefix: MetadataManager::OPENGRAPH_PREFIX], [uniq: bool = false]);

// or use the alias method :
Metadata::og( 'name', 'some OpenGraph value', [prefix: MetadataManager::OPENGRAPH_PREFIX], [uniq: bool = false]);

Generate the HTML tags

$html = Metadata::toHtml( [flags: bool = MetadataManager::ALL] );

Print HTML

Simply call the print method will echo the content returned by toHtml() :

Metadata::print( [flags: bool = MetadataManager::ALL] );

// or use the __toString magic method :
$metadata = new \Straylightagency\Metadata\MetadataManager;
$metadata->title( 'Page Title' );

echo $metadata; // print the meta title tag

Requirement

PHP 8.3 or above

See also

Credits

License

The MIT License (MIT).