Code Snippets

外掛說明

Code Snippets 是一個輕量化、簡單易用,專為在網站上執行程式碼片段所設計的解決方案。使用 Code Snippets 外掛後,便不再需要為了自訂程式碼片段,而必須修改使用中的佈景主題的 functions.php 檔案。

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

Code Snippets 提供了類似 [外掛] 頁面的圖形界面,藉以管理程式碼片段。加入 Code Snippets 外掛中的程式碼片段可以個別啟用或停用,就跟外掛一樣。程式碼片段編輯器包含可供網站管理員組織及管理程式碼片段的名稱欄位、可進行視覺化編輯的內容說明欄位、標籤欄位,以及功能完整的程式碼編輯器。現有的程式碼片段可以匯出,並移轉至其他網站;匯出的檔案為 JSON 及 PHP,前者用於在使用 Code Snippets 外掛的網站匯入,而後者可用於自行開發的外掛或佈景主題。

如有任何意見反應、問題或改進意見,請在外掛的技術支援論壇提出,或加入這個外掛的 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 的安全模式以復原網站。請參考 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 存放庫進行版本分支,並提出提取要求。

使用者評論

2022 年 6 月 14 日
Good, no problems, But it is irritating, that when a syntax error is created (by myself) that all changes are lost. (So before every save I copy the content of the snippet first)
2022 年 6 月 10 日
Users of this plugin already know how efficient and remarkable this one is. For those new to this, all I can say from experience of using Code Snippets on several of m websites - THANK YOU, team Code Snippets! Not having this plugin will show any Wordpress admin clearly the value it brings.
2022 年 6 月 6 日
The worst thing about the plugin is when you would like press <ctrl + s> and the finger presses <ctrl + w>.
2022 年 6 月 4 日
Never had any problems with it. It always did what I needed. Then one day I updated the plugin and it had a problem. I messaged the developer in the support forum on wordpress.org and he released a patch right away. Things have bugs from time to time so I decided to leave this 5 star review because of how quickly the developer released a patch for the bug I experienced and because I have this plugin installed on almost every site I use. It is my go to for adding snippets of code without having to change the functions.php
閱讀全部 377 則使用者評論

參與者及開發者

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

參與者

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

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

對開發相關資訊感興趣?

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

變更記錄

3.1.1 (13 Jun 2022)

  • Fixed: Download snippets feature not including snippet content.
  • Fixed: Alignment of ‘opens externally’ dashicon.
  • Improved: Added additional parameters to code_snippets/export/filename filter.

3.1.0 (17 May 2022)

  • Fixed: Caching inconsistencies preventing snippets and settings from refreshing on sites with persistent object caching.
  • Improved: Simplified database queries.
  • Added: More comprehensive cache coverage, including for active snippets.
  • Added: Icon to ‘Go Pro’ button indicating it opens an external tab.
  • Improved: Allow display styles in snippet descriptions.

3.0.1 (14 May 2022)

  • Fixed: Incompatibility issue with earlier versions of PHP.

3.0.0 (14 May 2022)

Added

  • Added: HTML content snippets for displaying as shortcodes or including in the page head or footer area.
  • Added: Notice reminding users to upgrade unsupported PHP versions.
  • Added: Visual settings to add attributes to shortcodes.
  • Added: Shortcode buttons to the post and page content editors.
  • Added: Basic REST API endpoints.
  • Added: Snippet type column to the snippets table.
  • Added: Snippet type badges to Edit and Add New Snippet pages.
  • Added: Setting to control whether the current line of the code editor is highlighted.
  • Added: Display a warning when saving a snippet with missing title or code.
  • Added: Add suffix to title of cloned snippets.

Changed

  • Improved: Updated plugin code to use namespaces, preventing name collisions with other plugins.
  • Improved: Added key for the ‘active’ and ‘scope’ database table columns to speed up queries.
  • Improved: Redirect from edit menu if not editing a valid snippet.
  • Improved: Moved activation switch into its own table column.
  • Improved: Updated code documentation according to WordPress standards.
  • Improved: Added snippet type labels to the tabs on the Snippets page.
  • Improved: Split settings page into tabs.
  • Improved: Use the version of CodeMirror included with WordPress where possible to inherit the additional built-in features.
  • Improved: Added hover effect to priority settings in the snippets table to show that they are editable.
  • Fixed: Snippets table layout on smaller screens.

Deprecated

  • Removed: Deprecated functions and compatibility code for unsupported PHP versions.
  • Removed: Option to disable snippet scopes.

New in Pro

  • Added: CSS style snippets for the site front-end and admin area.
  • Added: JavaScript snippets for the site head and body area on the front-end.
  • Added: Browser cache versioning for CSS and JavaScript snippets.
  • Added: Support for exporting and downloading CSS and JavaScript snippets.
  • Added: Support for highlighting code on the front-end.
  • Added: Editor syntax highlighting for CSS, JavaScript and HTML snippets.
  • Added: Button to preview full file when editing CSS or JavaScript snippets.
  • Added: Option to minify CSS and JavaScript snippets.
  • Added: Gutenberg editor block for displaying content snippets.
  • Added: Gutenberg editor block for displaying snippet source code.
  • Added: Elementor widget for displaying content snippets.
  • Added: Elementor widget for displaying snippet source code.

2.14.6 (13 May 2022)

  • Fixed: Issue with processing uploaded import files.
  • Fixed: Issue with processing tag filters.

2.14.5 (10 May 2022)

  • Fixed: Incompatibility issue with older versions of PHP.

2.14.4 (5 May 2022)

  • Fixed: Prevent array key errors when loading the snippet table with unknown order values.

2.14.3 (10 Dec 2021)

  • Fixed: Potential security issue outputting snippets-safe-mode query variable value as-is. Thanks to Krzysztof Zając for reporting.

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

The full changelog is available on GitHub