purplespider/silverstripe-basic-social-sharing

为Silverstripe网站添加社交媒体分享字段。

安装: 16

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 1

开放问题: 0

类型:silverstripe-vendormodule

1.1.0 2024-03-13 12:02 UTC

This package is auto-updated.

Last update: 2024-09-13 13:05:18 UTC


README

为Silverstripe网站添加社交媒体分享字段。

描述

本模块为SiteTree及其子页面类型添加了社交媒体元标签(OpenGraph:Facebook和Twitter)。它还在CMS中提供了标题、描述和图像字段,以便可以覆盖默认值。

模块将按照以下顺序寻找标题、描述和图像值:

  1. 在CMS提供的“社交媒体分享”部分中输入的字段。
  2. 在YML中配置的特定类的特定字段的值(见下文)。
  3. CMS“元数据”部分中适当字段的值。
  4. 模块配置中指定的“默认”字段的值,例如页面的“标题”和“内容”。

模块使用占位符(和字段描述)文本来指示正在使用哪些值,然后您可以选择性地覆盖它们。

支持的社会媒体元标签包括:

  • OpenGraph(Facebook)
    • og:site_name
    • og:title
    • og:image
    • og:description
    • og:url
  • Twitter
    • twitter:site
    • twitter:title
    • twitter:image
    • twitter:description
    • twitter:card

要求

SilverStripe 4.2+

安装

  1. composer require purplespider/silverstripe-basic-social-sharing ^1.0
  2. 运行/dev/build?flush=all
  3. 在没有配置的情况下,该模块将为所有页面类型开始渲染社交媒体元标签。
  4. 通过新“社交媒体分享”部分(位于页面现有“元数据”部分下方)对每个页面/DO进行自定义。

默认值

默认情况下,该模块将使用以下字段填充元标签:

  • og:type --> "website"
  • og:site_name --> SiteConfig.Title
  • og:title --> Title
  • og:description --> Content
  • og:url --> AbsoluteLink()
  • twitter:title --> Title
  • twitter:description --> Content
  • twitter:card --> "summary"

注意:twitter:card将默认设置为"summary",除非使用图像字段。如果使用图像字段,则内容将设置为"summary_large_image"

配置

通过编辑mysite/_config/config.yml来配置该模块,并设置以下选项:

PurpleSpider\BasicSocialSharing:
  default_title: 'Title' 
  default_description: 'Content' 
  twitter_site: '' 
  titles:	
    Fully\Namespaced\ClassName: 'FieldName'
    Fully\Namespaced\ClassName1: 'FieldName1'
  descriptions:
    Fully\Namespaced\ClassName: 'FieldName'
    Fully\Namespaced\ClassName1: 'FieldName1'
  images:	
    Fully\Namespaced\ClassName: 'FieldName'
    Fully\Namespaced\ClassName1: 'FieldName1'
  types:
    Fully\Namespaced\ClassName: 'website'
    Fully\Namespaced\ClassName1: 'article'
  • default_title - 覆盖默认标题字段以使用;默认使用PageTitle
  • default_description - 覆盖默认描述字段以使用;默认使用MetaDescription
  • twitter_site - 用于twitter:site元标签的内容。
  • titles - 一个类名数组,其值用于标题元标签的Text字段。对于向DataObjects添加社交媒体元标签输出非常有用。
  • descriptions - 一个类名数组,其值用于描述元标签的Text字段。对于向DataObjects添加社交媒体元标签输出非常有用。
  • images - 一个类名数组,其值用于图像元标签的Image字段。对于向DataObjects添加社交媒体元标签输出非常有用。
  • types - 一个类名数组,其值用于og:type元标签的内容。

示例 config.yml

PurpleSpider\BasicSocialSharing:
  default_title: 'PageTitle'
  default_description: ''
  titles:
    PurpleSpider\Models\Category: 'Name'
    PurpleSpider\Models\Product: 'Name'
    PurpleSpider\Models\Recipe: 'Name'
  descriptions:
    PurpleSpider\Models\Category: 'ShortDescr'
    PurpleSpider\Models\Product: 'Summary'
    PurpleSpider\Models\Recipe: 'MetaDescription'
  images:
    PurpleSpider\Pagetypes\AboutUsPage: 'LogoImage'
    PurpleSpider\Models\Category: 'BeautyShot'
    PurpleSpider\Models\Product: 'BeautyShot'
    PurpleSpider\Models\Recipe: 'Image'
  types:
    PurpleSpider\Models\Recipe: 'article'
  twitter_site: ''

Silverstripe博客模块支持

将以下内容添加到config.yml中:

PurpleSpider\BasicSocialSharing:
  images:
    SilverStripe\Blog\Model\BlogPost: "FeaturedImage"
  types:
    SilverStripe\Blog\Model\BlogPost: "article"

将社交媒体元标签添加到DataObjects

默认情况下,此模块仅将社交媒体元标签添加到SiteTree对象。

要将社交媒体元标签添加到DataObjects

  • _config/config.yml中,为要添加元标签的每个DataObject类添加扩展
Fully\Namespaced\Dataobject:
  extensions:
    - PurpleSpider\BasicSocialSharing\BasicSocialSharingExtension
  • _config/config.yml中,为您的类设置titlesdescriptionsimages(如有需要)的选项。
    • 例如,请参阅上面的config.yml示例。
  • 在您的DataObject类中,在getCMSFields()中添加新字段
  public function getCMSFields() {
      $fields = parent::getCMSFields();
      
      ...
      
      $fields->addFieldToTab('Root.Main', new TextareaField("SocialMetaDescription"));
      $fields->addFieldToTab('Root.Main', new UploadField("SocialMetaImage"));
      
      return $fields;
  }
  • 运行/dev/build?flush=all

许可协议

本项目遵循新BSD许可证

原始模块

这是对https://github.com/innis-maggiore/silverstripe-social-meta-tags的深度修改和更具意见版本。衷心感谢Innis对关键功能的开发。