冬季 / wn-seo-plugin
Winter CMS SEO 标签管理插件
dev-main
2024-02-20 16:46 UTC
Requires
- php: >=7.2
- composer/installers: ~1.11
This package is auto-updated.
Last update: 2024-09-20 17:58:08 UTC
README
轻松处理 Winter CMS 项目中的搜索引擎优化。灵感来源于 https://github.com/bennothommo/wn-meta-plugin。
未来计划包括支持轻松生成结构化数据以及自动将 SEO 元字段附加到 CMS 页面、Winter.Pages 页面以及任何 Winter CMS 模型。请查看 Plugin.php 中的 TODO 列表以获取更多计划功能。
安装
composer require winter/wn-seo-plugin
然后在您的主题的 <head>
中添加 [seoTags]
组件,理想情况下在标准编码和响应式标签之后。
建议实现
布局或头部部分:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> {% partial "meta/seo" %} </head>
partials/meta/seo.htm
:
[seoTags] == <?php use Backend\Models\BrandSetting; use System\Classes\MediaLibrary; use Winter\SEO\Classes\Link; use Winter\SEO\Classes\Meta; function onStart() { $this['page_title'] = $this->page->title ?? Meta::get('og:title') ?? ''; $this['app_name'] = BrandSetting::get('app_name'); // Set the cannonical URL Link::set('canonical', \URL::current()); // Parse the meta_image as a media library image if (!empty($this->page->meta_image)) { $this->page->meta_image = MediaLibrary::url($this->page->meta_image); } // Handle the nofollow meta property being set if (!empty($this->page->meta_nofollow)) { Link::set('robots', 'nofollow'); } // Set the meta tags based on the current page if not set $metaMap = [ Meta::class => [ 'og:title' => 'meta_title', 'og:description' => 'meta_description', 'og:image' => 'meta_image', ], Link::class => [ 'prev' => 'paginatePrev', 'next' => 'paginateNext', ], ]; foreach ($metaMap as $class => $map) { foreach ($map as $name => $pageProp) { if (!empty($this->page->{$pageProp}) && empty($class::get($name))) { $class::set($name, $this->page->{$pageProp}); } } } $this['raw_title'] = Meta::get('title'); } ?> == <title> {%- placeholder page_title default %} {%- if raw_title %}{{ raw_title | striptags }}{% elseif page_title %}{{ page_title | striptags }} | {{ app_name }}{% else %}{{ app_name }}{% endif -%} {% endplaceholder -%} </title> {% component seoTags %}
配置
此插件的配置通过一个 配置文件 处理。为了修改配置值并开始使用,您可以将 plugins/winter/seo/config/config.php
文件复制到 config/winter/seo/config.php
,并在那里进行更改。
使用
元标签
使用 Meta
类添加将由 [seoTags]
组件渲染的 <meta>
标签。示例
use Winter\SEO\Classes\Meta; // Adds <meta name="og:type" content="article"> Meta::set('og:type', 'article'); // Appends a meta tag to the collection; allowing for full control of the // attributes used as well as preventing it from being overridden and / or // enabling multiple tags with the same name to be added. Meta::append([ 'name' => 'og:type', 'content' => 'article', 'example_attribute' => 'the_cake_is_a_lie', ]); // Overrides `og:type` because it was set later in the request Meta::set('og:type', 'article'); // Retreive a specific meta tag by its name Meta::get('og:type'); // Retrieve all meta tags currently set in this request Meta::all() // Clear all previously set meta tags and start fresh from this point on in the request Meta::refresh();
链接标签
使用 Link
类添加将由 [seoTags]
组件渲染的 <link>
标签。示例
use Winter\SEO\Classes\Link; // Adds <link rel="base_url" rel="https://example.com"> Link::set('base_url', 'https://example.com'); // Appends a link tag to the collection; allowing for full control of the // attributes used as well as preventing it from being overridden and / or // enabling multiple tags with the same name to be added. Link::append([ 'rel' => 'preload', 'href' => 'https://example.com/logo.png', 'as' => 'image', ]); // Overrides `base_url` because it was set later in the request Link::set('base_url', url()->current()); // Retreive a specific link tag by its name Link::get('base_url'); // Retrieve all link tags currently set in this request Link::all() // Clear all previously set link tags and start fresh from this point on in the request Link::refresh();