bit-part/craft-code-snippets 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bit-part/craft-code-snippets

最新稳定版本:1.0.0

Composer 安装命令:

composer require bit-part/craft-code-snippets

包简介

Manage and inject code snippets (GTM, Analytics, ad tags, etc.) into your Craft CMS site from the control panel.

README 文档

README

日本語 | Deutsch

Manage and inject code snippets (GTM, Analytics, ad tags, chat widgets, etc.) into your Craft CMS site directly from the control panel — no template editing required.

Requirements

  • Craft CMS 5.0.0 or later
  • PHP 8.2 or later

Installation

Via Composer (recommended)

composer require bit-part/craft-code-snippets

Then install the plugin from the Craft control panel under Settings > Plugins, or via the CLI:

php craft plugin/install code-snippets

Manual Installation

  1. Download the release from GitHub
  2. Place the contents in a directory and add a path repository to your project's composer.json
  3. Run composer require bit-part/craft-code-snippets
  4. Install via the control panel or CLI

How It Works

Code Snippets automatically injects your managed code into the rendered HTML output. No template changes are needed by default.

  1. Add a snippet in the control panel (e.g., a Google Analytics tracking script)
  2. Choose an insertion position: <head> start, </head> end, <body> start, or </body> end
  3. Optionally restrict by environment (dev, staging, production) or URI pattern
  4. The plugin injects active snippets into every matching page

Features

Snippet Management

A dedicated Code Snippets section in the control panel lets you:

  • Create, edit, and delete snippets
  • Enable/disable snippets with bulk actions
  • Duplicate existing snippets for quick setup across environments
  • Set insertion position (4 positions available)
  • Filter snippets by position using the sidebar
  • Search, sort, and export snippets

The snippet list uses Craft's native element index, providing the same familiar interface as Entries and Assets.

Insertion Positions

Position Inserts at Typical use
<head> Start Right after <head> GTM, high-priority scripts
</head> End Right before </head> Analytics, meta tags
<body> Start Right after <body> GTM noscript fallback
</body> End Right before </body> Chat widgets, deferred scripts

Auto-Injection (Default)

Snippets are automatically injected into rendered HTML without any template changes. The plugin hooks into Craft's template rendering pipeline and inserts your code at the correct positions.

This is ideal for marketers and content editors who need to add tracking codes without developer assistance.

Twig Functions (Optional)

For developers who prefer explicit control, disable auto-injection in settings and use Twig functions in your layout template:

{{ craft.codeSnippets.headBegin() }}
{{ craft.codeSnippets.headEnd() }}
{{ craft.codeSnippets.bodyBegin() }}
{{ craft.codeSnippets.bodyEnd() }}

Environment Filtering

Restrict snippets to specific environments. For example, keep debug scripts on dev only, or ensure production tracking codes don't fire in development.

Select one or more environments (dev, staging, production) per snippet. Leave all unchecked to run on all environments.

URI Pattern Matching

Target specific pages using URI patterns with wildcard support:

Pattern Matches
checkout /checkout only
products/* /products/shoes, /products/hats/red, etc.
* All pages
(empty) All pages

Enter one pattern per line. Leave empty to match all pages.

Sort Order

Control the order of multiple snippets in the same position using the sort order field. Lower numbers appear first.

Settings

Navigate to Settings > Plugins > Code Snippets to configure:

Setting Default Description
Auto Inject On Automatically inject snippets into rendered HTML. When disabled, use the Twig functions instead.

Permissions

The plugin registers two permissions under Code Snippets:

Permission Description
View snippets Access the Code Snippets section in the control panel
Manage snippets Create, edit, delete, and toggle snippets. Note: This permission allows injecting arbitrary HTML/JavaScript into the site. Grant only to trusted users.

Permissions are nested: "Manage snippets" requires "View snippets" to be granted first.

Security

This plugin intentionally outputs user-provided HTML/JavaScript code on the frontend. This is the core functionality — similar to Google Tag Manager, WordPress Header & Footer Scripts, and other snippet injection tools.

Important: Only grant the "Manage snippets" permission to trusted administrators. Users with this permission can inject arbitrary scripts into your site.

Translations

The plugin includes translations for:

  • English (en)
  • Japanese (ja)

Support

License

This plugin is licensed under the Craft License.

Built by bit part LLC

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: proprietary
  • 更新时间: 2026-04-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固