Code Snippets

外掛說明

Code Snippets is an easy, clean and simple way to run code snippets on your site. It removes the need to add custom snippets to your theme’s functions.php file.

程式碼片段是一小段可用來擴充 WordPress 網站功能的 PHP 程式碼,本質上就是一個對網站主機負擔較小的微型外掛。
大多數提供程式碼片段的網站,都會告訴使用將程式碼片段加入網站目前使用的佈景主題的 functions.php 檔案中,但這會導致在一段時間後,檔案變得又長又混亂。
Code Snippets 外掛為新增程式碼片段提供了使用者介面,並讓在外掛中加入的程式碼片段,執行起來的結果跟將程式碼片段加入佈景主題的 functions.php 檔案一模一樣。

Code Snippets provides graphical interface, similar to the Plugins menu, for managing snippets. Snippets can be activated and deactivated, just like plugins. The snippet editor includes fields for a name, a visual editor-enabled description, tags to allow you to categorize snippets, and a full-featured code editor. Snippets can be exported for transfer to another site, either in JSON for later importing by the Code Snippets plugin, or in PHP for creating your own plugin or theme.

如有任何意見反應、問題或改進意見,請在外掛的技術支援論壇提出,或加入這個外掛的 Facebook 私密社團

如果喜歡這個外掛,或外掛對網站確實有幫助,請在 WordPress.org 為它進行評等。

如果想要參與這個外掛的開發,或要將這個外掛本地化為你的母語,可以將外掛的 GitHub 存放庫進行分支。

語言套件

感謝下列本地化人員,讓 Code Snippets 可以顯示多國語言介面。

螢幕擷圖

  • 管理現有的程式碼片段
  • 新增程式碼片段
  • 編輯程式碼片段
  • 從匯出檔匯入程式碼片段

安裝方式

自動安裝

  1. 登入 WordPress 網站管理後台
  2. 點擊 [外掛]
  3. 點擊 [安裝外掛]
  4. 搜尋「Code Snippets」
  5. 點擊 Code Snippets 外掛中的 [立即安裝]
  6. 啟用外掛

手動安裝

  1. 下載外掛安裝套件 ZIP 壓縮檔
  2. 解壓縮外掛的 ZIP 壓縮檔
  3. 將解壓縮外掛的 ZIP 壓縮檔所得的 code-snippets 資料夾上傳至 WordPress 網站的 wp-content/plugins/ 目錄
  4. 在 [外掛] 頁面中啟用 Code Snippets 外掛

在多站網路控制台中為多站網路啟用 Code Snippets 外掛,會顯示一個特殊介面,用於標示執行於整個多站網路的程式碼片段。

常見問題集

請參考 help.codesnippets.pro 以取得常見問題集的完整清單。

如何將程式碼片段插入至文章內容編輯器中?

Code Snippets v2 設計用來使用 PHP 函式程式碼片段,這些程式碼片段不是用來插入內容中,而是插入網站程式碼中持續執行,這類程式碼片段比較類似外掛或佈景主題的 PHP 檔案。

Code Snippets v3 會帶來內容程式碼片段這種全新類型的程式碼片段,這種程式碼片段可用來插入文章及頁面的內容中,敬請期待新版外掛的發佈。

在執行有程式碼錯誤的程式碼片段導致網站無法正常執行後,如何復原網站?

網站管理員可以啟用 Code Snippets 的安全模式復原。請參考 https://help.codesnippets.pro/article/12-safe-mode 以了解啟用安全模式的方式。

如果變更佈景主題或升級 WordPress,現有的程式碼片段是否會失效?

不會。在這個外掛中輸入的程式碼片段,均會儲存於資料庫中,既獨立於佈景主題之外,也不受 WordPress 更新的影響。

這個外掛是否能完整解除安裝?

如果在外掛的設定頁面中啟用 [完整解除安裝] 設定,在 WordPress 的 [外掛] 選單中刪除 Code Snippets 時,並會清除全部資料。請注意,這項設定會清除包含儲存於資料庫中的程式碼片段的全部資料。如需保留之前建立的程式碼片段,請先執行匯出程序。

是否能使用已為其他 WordPress 網站建立的程式碼片段?

可以。在 [全部程式碼片段] 頁面中,網站管理員可以使用程式碼片段名稱下方的 [匯出] 連結匯出個別程式碼片段,或是使用 [批次操作] 功能批次匯出多個程式碼片段。已匯出的程式碼片段稍後可以在其他網站透過 [匯入程式碼片段] 頁面,上傳程式碼片段檔案並匯入。

能將現有的程式碼片段匯出為 PHP 程式碼,以供未採用 Code Snippets 外掛的網站使用嗎?

可以。點擊想要匯出的程式碼片段旁的核取方塊,然後從 [批次操作] 選單中選取 [匯出為 PHP] 並點擊 [套用],這項操作產生的 PHP 檔案會包含匯出的程式碼片段,以及名稱及內容說明。

能為多站網路中的全部網路執行同一份程式碼片段嗎?

在多站網路控制台中,可以透過多站網路啟用功能將某份程式碼片段啟用於多站網路中的全部網站;多站網路管理員能將 Code Snippets 外掛啟用於全部網站,也能在個別網站啟用外掛。

程式碼片段都儲存在 WordPress 網站資料庫的哪個位置?

程式碼片段儲存於 WordPress 網站資料庫的 wp_snippets 資料表中。請注意,這個資料表的名稱實際上可能略有不同,主要取決於安裝網站時的資料表前置詞設定。

在何處可以提出新功能建議?

請在 Code Snippets 外掛的 WordPress 技術支援論壇GitHub 存放庫回報程式碼錯誤、新功能建議及改進意見。

如何參與 Code Snippets 外掛的開發?

先感謝你有這樣的意願。每個人都可以為外掛的 GitHub 存放庫進行版本分支,並提出提取要求。

使用者評論

2021 年 9 月 16 日
I've been working on a large project that uses a lot of filters and hooks, etc. and I've been using a custom plugin to handle all of it, but as the project grew the plugin got larger and more complicated and harder to maintain or change. This great little tool let me separate all those functions into nice, small, modular snippets that each do a single task. Cleans everything up beautifully, and makes the project so much easier to maintain. Thanks for creating this, I was almost ready to do something like it myself!
2021 年 9 月 16 日
I use it on pretty much every site! If this plugin doesn't work for you, then it's your fault, not the plugins fault!
2021 年 9 月 14 日
Simple, complete, and easy to use.
2021 年 9 月 14 日
If you want to add some PHP or JavaScript to your site, this makes it pretty easy. I have issues only of my own making, where my management of my code hasn't been strong enough to control the single version of code supported by this plugin. I recommend that you keep a parallel system to track your code and its changes and use this plugin to activate your code on your WordPress site.
2021 年 9 月 7 日
For years this has been my go-to plugin and the easiest and safest way to add PHP and JS code snippets on countless websites. First of all - reliability: I know it won't break the website even if there's an error in the code. And second, unlike many other popular plugins, it has no annoying upsells and no obtrusive banners. And lastly - no bloat. Reliable, clean and fast - This is something that every plugin should strive to. Keep it up.
閱讀全部 316 則使用者評論

參與者及開發者

以下人員參與了開源軟體〈Code Snippets〉的開發相關工作。

參與者

Code Snippets 外掛目前已有 17 個本地化語言版本。 感謝全部譯者為這個外掛做出的貢獻。

將 Code Snippets 外掛本地化為台灣繁體中文版

對開發相關資訊感興趣?

任何人均可瀏覽程式碼、查看 SVN 存放庫,或透過 RSS 訂閱開發記錄

變更記錄

2.14.2 (9 Sep 2021)

  • Fixed: Prevent network snippets table from being created on single-site installs.
  • Added translations:
  • Added: Support for :class syntax to the code validator.
  • Added: PHP8 support to the code linter.
  • Added: Color picker feature to the code editor.
  • Added: Failsafe to prevent multiple versions of Code Snippets from running simultaneously.

2.14.1 (10 Mar 2021)

  • Added: Czech translation by Lukáš Tesař.
  • Fixed: Code validator now supports function_exists and class_exists checks.
  • Fixed: Code validator now supports anonymous functions.
  • Fixed: Issue with saving the hidden columns setting.
  • Fixed: Replaced the outdated tag-it library with tagger for powering the snippet tags editor.
  • Added: Code direction setting for RTL users.
  • Updated CodeMirror to version 5.59.4.
  • Added: Additional action hooks and search API thanks to @Spreeuw.

2.14.0 (26 Jan 2020)

  • Updated CodeMirror to version 5.50.2.
  • Added: Basic error checking for duplicate functions and classes.
  • Updated Italian translations to fix display issues – thanks to Francesco Marino.
  • Fixed: Ordering snippets in the table by name will now be case-insensitive.
  • Added: Additional API options for retrieving snippets.
  • Fixed: Code editor will now properly highlight embedded HTML, CSS and JavaScript code.
  • Changed the indicator color for inactive snippets from red to grey.
  • Fixed a bug preventing the editor theme from being set to default.
  • Added: Store the time and date when each snippet was last modified.
  • Added: Basic error checking when activating snippets.
  • Fixed: Ensure that imported snippets are always inactive.
  • Fixed: Check the referer on the import menu to prevent CSRF attacks. Thanks to Chloe with the Wordfence Threat Intelligence team for reporting.
  • Fixed: Ensure that individual snippet action links use proper verification.

2.13.3 (13 Mar 2019)

  • Added: Hover effect to activation switches.
  • Added: Additional save buttons above snippet editor.
  • Added: List save keyboard shortcuts to the help tooltip.
  • Added: Change “no items found” message when search filters match nothing.
  • Fixed: Calling deprecated code in database upgrade process.
  • Fixed: Include snippet priority in export files.
  • Fixed: Use Unix newlines in code export file.
  • Updated CodeMirror to version 5.44.0.
  • Fixed: Correctly register snippet tables with WordPress to prevent database repair errors [#]
  • Fixed: CodeMirror indentation settings being applied incorrectly

2.13.2 (25 Jan 2019)

  • Removed potentially problematic cursor position saving feature

2.13.1 (22 Jan 2019)

  • Added: Add menu buttons to settings page for compact menu
  • Updated: French translation updated thanks to momo-fr
  • Fixed: Split code editor and tag editor scripts into their own files to prevent dependency errors
  • Fixed: Handling of single-use shared network snippets
  • Fixed: Minor translation template issues
  • Added: Help tooltop to snippet editor for keyboard shortcuts, thanks to Michael DeWitt
  • Improved: Added button for executing single-use snippets to snippets table
  • Added: Sample snippet for ordering snippets table by name by default
  • Updated CodeMirror to version 5.43.0

2.13.0 (17 Dec 2018)

  • Added: Search/replace functionality to the snippet editor. See here for a list of keyboard shortcuts. [#]
  • Updated CodeMirror to version 5.42.0
  • Added: Option to make admin menu more compact
  • Fixed: Problem clearing recently active snippet list
  • Improved: Integration between plugin and the CodeMirror library, to prevent collisions
  • Improved: Added additional styles to editor settings preview
  • Added: PHP linter to code editor
  • Improved: Use external scripts instead of inline scripts
  • Fixed: Missing functionality for ‘Auto Close Brackets’ and ‘Highlight Selection Matches’ settings

2.12.1 (15 Nov 2018)

  • Improved: CodeMirror updated to version 5.41.0
  • Improved: Attempt to create database columns that might be missing after a table upgrade
  • Improved: Streamlined upgrade process
  • Fixed: Interface layout on sites using right-to-left languages
  • Improved: Made search box appear at top of page on mobile [#]
  • Updated screenshots

2.12.0 (23 Sep 2018)

  • Fixed: Prevented hidden columns setting from reverting to default
  • Improved: Updated import page to improve usability
  • Improved: Added Import button next to page title on manage page
  • Improved: Added coloured banner indicating whether a snippet is active when editing
  • Update CodeMirror to 5.40.0

2.11.0 (24 Jul 2018)

  • Added: Ability to assign a priority to snippets, to determine the order in which they are executed
  • Improvement: The editor cursor position will be preserved when saving a snippet
  • Added: Pressing Ctrl/Cmd + S while writing a snippet will save it
  • Added: Shadow opening PHP tag above the code editor
  • Improved: Updated the message shown when there are no snippets
  • Added: Install sample snippets when the plugin is installed
  • Improved: Show all available tags when selecting the tag field
  • Added: Filter hook for controlling the default list table view
  • Added: Action for cloning snippets

2.10.2 (21 Jul 2018)

  • Added: Button to reset settings to their default values
  • Improved: Made uninstall cleanup optional through a plugin setting
  • Fixed: Applied formatting filters to snippet descriptions in the table
  • Improved: Ordered tags by name in the filter dropdown menu
  • Fixed: Incorrectly translated strings
  • Added: Belarusian translation by Hrank.com
  • Improved: Enabled sorting snippets table by tags
  • Updated CodeMirror to version 5.39.0

2.10.1 (10 Feb 2018)

  • Fixed: Prevent errors when trying to export no snippets
  • Fixed: Use wp_json_encode() to encode export data
  • Fixed: Check both the file extension and MIME type of uploaded import files

2.10.0 (18 Jan 2018)

  • Improved: Added support for importing from multiple export files at once
  • Improved: Unbold the titles of inactive snippets for greater visual distinction
  • Added: New scope for single-use snippets
  • Improved: Don’t show network snippets on subsites by default, and only to super admins
  • Improved: Export snippets to JSON instead of XML
  • Improved: More options for importing duplicate snippets
  • Improved: Use strings for representing scopes internally instead of numbers
  • Added: Allowed plugin settings to be unified on multisite through Network Settings option
  • Fixed: Issue with incorrectly treating network snippets as site-wide for code validation
  • Improved: Rename ‘Export to PHP’ to ‘Download’, and add button to edit snippet page

The full changelog is available on GitHub