pedroborges / kirby-meta-tags Goto Github PK
View Code? Open in Web Editor NEW⬢ HTML meta tags generator for Kirby. Supports Open Graph and Twitter Cards out of the box.
License: MIT License
⬢ HTML meta tags generator for Kirby. Supports Open Graph and Twitter Cards out of the box.
License: MIT License
Passing $kirby
to pedroborges.meta-tags.default|templates
is helpful.
I.e. to differ metadata on the route used:
<?php
$config = array(
// ...
'pedroborges.meta-tags.default' => function ($page, $site, $kirby) {
$template = $page->template()->name();
if ($template == 'blog') {
$isArchive = $kirby->route()->attributes()['pattern'] == 'blog/(:any)';
if ($isArchive) {
$description = '<Category Archive Description>';
} else {
$description = '<Blog Index Description>';
}
}
return [
// ...
'meta' => [
'description' => $description
],
]
);
In the current implementation, the first instance of MetaTags gets cached and and this instance gets reused even if one requests a MetaTags instance for a different page.
metaTags($pageA) // creates an instance and caches it as static variable
metaTags($pageB) // returns this very same object
Instead a new instance should be created for each page and maybe cached separately
The documentation refers to template-specific information, e.g. the song duration. As to my understanding, this breaks quickly when some pages (even with other templates) do not have these fields. Probably this is because the config is generally evaluated with such specific code not just being run for pages with the specific template but for all.
I have therefore added a short check in my configuration, e.g.
...
'og' => [ // tags group name
'type' => 'article', // overrides the default
'locale' => $site->language()->code(),
'namespace:article' => [
'author' => ( ($page->author() !== null && $page->author()->toUser() !== null) ? function($page, $site) { $websites=[]; foreach ($page->author()->toUsers() as $user) $websites[] = $user->website(); return $websites; } : ''),
'published_time' => $page->date('Y-m-d'),
'modified_time' => $page->modified('Y-m-d'),
'tag' => ($page->tags()->isNotEmpty()) ? $page->tags()->split() : '',
],
...
I think this could be wise to add to the documentation.
Hi Pedro,
there's a way to get this working?:
$page->children()->find('contact-info')->email()
I can't access to this field using children()->find()
.
Thanks for the great plugin!
So I have an og tag defined as follows:
'tag' => $page->tags()->split(',')
If I have a tag of 'time' I get the following error
Whoops\Exception\ErrorException: time() expects exactly 0 parameters, 2 given in file /Applications/MAMP/htdocs/cms-dev/site/plugins/meta-tags/src/MetaTags.php on line 106
Is there a way I can escape this? I have tried to do the following
'tag' => $page->tags()->kt()->split(',')
But this then returns
<meta property="og:article:tag" content="time</p>">
Which is wrong as it should just say 'time' within the content.
If I change the tag from 'time' to 'times' then it works, so it specifically relates to the word 'time' and I'm guessing it's already a function within Kirby.
The description on how to use language links seems wrong to me. As far as I can tell, it would need to refer to $site->kirby()->languages() instead of $site->languages() (the latter returns null in my installation).
I get the following error when you using the the update to composer added today (v2.0).
Method Illuminate\View\View::__toString() must not throw an exception, caught ErrorException: Argument 1 passed to metaTags() must be an instance of Page, instance of Kirby\Cms\Page given, called in /public/site/plugins/meta-tags/index.php on line 8 (View: /public/site/templates/global/metatags.blade.php)
Im using Kirby 3.0.2, and the Blade plugin, but meta tags worked before todays updated that added composer and a few fixes.
Thanks for great plugin!
I use it for my multilingual website and I think that the right code for link to alternative content is:
'alternate' => function(Page $page, Site $site) {
$locales = [];
foreach ($site->languages() as $language) {
if ($language->code() == $site->language()) continue;
$locales[] = [
'hreflang' => $language->code(),
'href' => $page->url($language->code())
];
}
return $locales;
}
instead
'alternate' => function(Page $page, Site $site) {
$locales = [];
foreach ($site->languages() as $language) {
if ($language->isDefault()) continue;
$locales[] = [
'hreflang' => $language->code(),
'href' => $page->url($language->code())
];
}
return $locales;
}
First: great plugin. Thx!
You state in the readme for tag groups:
These groups accept string, closure, or array as their values. Being so flexible, the sky is the limit to what you can do with Meta Tags!
Can you provide a working example of that please?
I've tried the following to no avail:
c::set('meta-tags.templates', [
'article' => function($page, $site) {
return [
"title" => "test",
'og' => [
'title' => "test"
]
];
},
'about' => [ /* tags groups */ ],
'products' => [ /* tags groups */ ],
]);
I've tried this on a article
, but I'm getting the title
and og:title
as defined in the meta-tags.default
. It seems like the array_merge isn't happening?
When using an array instead of a closure it works:
c::set('meta-tags.templates', [
'article' => [
"title" => "test",
'og' => [
'title' => "test"
]
],
'about' => [ /* tags groups */ ],
'products' => [ /* tags groups */ ],
]);
So either the readme is off, or the closure functionality is failing?
I really need closures to work, as I need template-specific variables in the meta tags which means I cannot "pre-populate" the meta-tags.templates
array since some variables are only available in specific templates.
Just tried the plugin in the last PlainKit Beta and it throws an "Illegal offset type in isset or empty" error on line 56 of the plugin.
if (isset($templates[$page->template()])) {
I tried PHP 7.0, 7.1 and 7.2. Same error.
I'm trying to implement alternate links on multilingual setup from your readme example and I get Undefined variable: site
.
Any suggestions?
Thanks
Missing composer.json version
With the Kirby 3 plugin, there seems to be a difference in how template settings are combined with default settings.
Consider:
c::set('pedroborges.metatags.default', function($page, $site) {
return [
'og' => [
'title' => $page->metatitle(),
'type' => 'website',
'site_name' => $site->title(),
'url' => $page->url(),
'description' => $page->metadescription(),
'locale' => explode('.', kirby()->language()->locale())[0],
'namespace:image' => [...],
],
];
});
Coupled with:
c::set('pedroborges.metatags.templates', function($page) {
return [
'project' => [
'og' => [
'namespace:image' => [...],
],
],
];
});
The og
tags on a page with a template project
contains only those defined in the templates settings, not the defaults.
With the Kirby 2 version, the template specific settings used to be merged with the default. Now they seems to simply replace them.
Hi Pedro.
Thanks for the update to this plugin, but I noticed that the version number in this file was not updated to match the readme or the package.json files.
Line 6 in e7804c3
As the title says. If you set JSON-LD up in meta-tags.default it comes out on every page. If you want a different schema on a specific page (in my case blog post schema), this block also comes out on every page, despite being set within meta-tags.templates
for a specific page template.
Other properties in meta-tags.templates
work as expected, seems limited to just JSON-LD.
How can I make this work?
So I have a meta-tags.templates setup for a note template/page, with an og property of article:author - this is currently working when on an actual note template/page, but the moment I go to any other template/page it breaks and fails on call to a member function name() on null this shouldn't even be firing or trying to be fired on any template/page other than the note template/page. If I remove the toUser() it works, but I don't get the objects I require. So it seems to be related to the addition of toUser() into the call, but as I've mentioned why is this even firing or trying to be fired on any other pages? It should only ever be fired on the note template/page.
The breaking code is as follows:
'pedroborges.meta-tags.templates' => function ($page, $site) {
return [
'note' => [
'og' => [
'namespace:article' => [
'author' => $page->author()->toUser()->name()
]
]
]
]
}
I would like to use multiple "prefetch" and "preconnect" links:
e.g.:
c::set('meta-tags.default', function(Page $page, Site $site) {
return [
'link' => [
'prefetch' => "https://use.typekit.net",
'preconnect' => "https://use.typekit.net"
'prefetch' => "https://some.other-domain.tld",
'preconnect' => "https://some.other-domain.tld"
],
});
Only the last are being printed :-S
Hi!
I was testing the waters for Kirby 3.9 RC1 and PHP8.2 and encountered a deprecation notice that I thought might be important to report:
Whoops\Exception\ErrorException thrown with message "Creation of dynamic property PedroBorges\KirbyMetaTags\MetaTags::$data is deprecated"
Stacktrace:
Whoops\Exception\ErrorException in /site/plugins/meta-tags/src/MetaTags.php:45
More information about this deprecation: https://stitcher.io/blog/deprecated-dynamic-properties-in-php-82
That's all, have a nice day.
Hi Pedro,
there's a way to create collections with more than one item similar to this: https://jsonld-examples.com/schema.org/code/blog-markup.php#advancedCode?
Thanks.
Calling render multiple times creates causes tags to be copied for each call.
So for example.
$meta = metaTags(page());
echo $meta->render(); // => <title>test</title>
echo $meta->render(); // => <title>test</title><title>test</title>
echo $meta->render(); // => <title>test</title><title>test</title><title>test</title>
It does not matter if you pass the group argument to the function. It still copies all the tags with each call
Of course the render method should not modify the result like this and should not have side effects.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.