|
| 1 | +# CloudScale Code Block |
| 2 | + |
| 3 | +    |
| 4 | + |
| 5 | +Proper syntax highlighted code blocks for WordPress. Automatic language detection, one click copy to clipboard, dark and light theme toggle, line numbers, full width responsive display. Works as both a Gutenberg block and a shortcode. Zero build step required. |
| 6 | + |
| 7 | +No webpack. No npm install. No node modules. Upload and activate. |
| 8 | + |
| 9 | +> Full write up: [Building a Better Code Block for WordPress](https://andrewbaker.ninja/2026/02/27/building-a-better-code-block-for-wordpress-cloudscale-code-block-plugin/) |
| 10 | +
|
| 11 | +## Features |
| 12 | + |
| 13 | +- **Syntax highlighting** via highlight.js 11.11.1 with 28 languages out of the box |
| 14 | +- **Automatic language detection** with manual override per block |
| 15 | +- **Copy to clipboard** button on every code block |
| 16 | +- **Dark/light theme toggle** (Atom One Dark and Atom One Light) |
| 17 | +- **Line numbers** for easy reference |
| 18 | +- **Optional title bar** for filenames or descriptions |
| 19 | +- **Gutenberg block** registered under the Formatting category |
| 20 | +- **Shortcode** (`cs_code`) for classic editor or non Gutenberg contexts |
| 21 | +- **On demand asset loading**: highlight.js and theme CSS only load on pages with code blocks |
| 22 | +- **Server side rendering** via PHP callback, immune to Gutenberg markup validation errors |
| 23 | + |
| 24 | +### Supported Languages |
| 25 | + |
| 26 | +Bash, C, C++, C#, CSS, Diff, Docker, Go, GraphQL, HTML/XML, INI, Java, JavaScript, JSON, Kotlin, Makefile, Markdown, Nginx, PHP, PowerShell, Python, Ruby, Rust, SCSS, Shell, SQL, TypeScript, YAML. |
| 27 | + |
| 28 | +## The Gutenberg Paste Problem |
| 29 | + |
| 30 | +When you paste markdown with fenced code blocks, Gutenberg intercepts the paste and creates `core/code` blocks before any plugin can touch it. CloudScale detects this and shows a floating toast: |
| 31 | + |
| 32 | +> 2 core code blocks found **Convert All to CloudScale** |
| 33 | +
|
| 34 | +One click converts every `core/code` and `core/preformatted` block in the post to CloudScale Code Blocks, preserving all code content. Paste your markdown, click Convert All, done. |
| 35 | + |
| 36 | +## Bulk Migration |
| 37 | + |
| 38 | +If you have existing posts using WordPress default code blocks or the Code Syntax Block plugin, the built in migrator handles bulk conversion. |
| 39 | + |
| 40 | +Go to **Tools > Code Block Migrator**: |
| 41 | + |
| 42 | +1. **Scan Posts** finds every post containing legacy code blocks |
| 43 | +2. **Preview** shows side by side comparison of original and converted markup |
| 44 | +3. **Migrate This Post** converts a single post, or **Migrate All Remaining** batch converts everything |
| 45 | + |
| 46 | +The migrator handles three block types: `wp:code` (WordPress default), `wp:code-syntax-block/code` (Code Syntax Block plugin), and `wp:preformatted` (preformatted text blocks). Language hints are extracted from attributes and CSS classes. Code content is never modified. |
| 47 | + |
| 48 | +## Requirements |
| 49 | + |
| 50 | +- WordPress 6.0 or higher |
| 51 | +- PHP 8.0 or higher |
| 52 | + |
| 53 | +## Installation |
| 54 | + |
| 55 | +1. Download the latest release zip from the [Releases](../../releases) page |
| 56 | +2. In WordPress admin go to **Plugins > Add New > Upload Plugin** |
| 57 | +3. Upload the zip file, click **Install Now**, then **Activate Plugin** |
| 58 | +4. The block appears in the Gutenberg inserter under **Formatting** |
| 59 | +5. Settings at **Settings > CloudScale Code** |
| 60 | + |
| 61 | +### Upgrading |
| 62 | + |
| 63 | +Deactivate > Delete > Upload zip > Activate. |
| 64 | + |
| 65 | +## Configuration |
| 66 | + |
| 67 | +**Default theme**: Set dark or light at **Settings > CloudScale Code**. Individual blocks can override via the sidebar inspector. |
| 68 | + |
| 69 | +**Language**: Auto detected by default. Override per block with the language selector (28 languages). |
| 70 | + |
| 71 | +**Title**: Optional title bar above the code block, useful for filenames. |
| 72 | + |
| 73 | +## Technical Details |
| 74 | + |
| 75 | +The plugin registers a single Gutenberg block (`cloudscale/code-block`) with a PHP render callback. Block data is stored as three attributes: `content` (raw code), `language` (optional), and `title` (optional). The block uses `save: function() { return null; }` so all rendering is server side, making it resilient to markup changes and avoiding the "unexpected or invalid content" validation error. |
| 76 | + |
| 77 | +The auto convert watcher uses `wp.data.subscribe` to monitor the block store. Conversion calls `wp.data.dispatch('core/block-editor').replaceBlock()` to swap blocks while preserving content. |
| 78 | + |
| 79 | +## License |
| 80 | + |
| 81 | +GPLv2 or later. See [LICENSE](LICENSE) for the full text. |
| 82 | + |
| 83 | +## Author |
| 84 | + |
| 85 | +[Andrew Baker](https://andrewbaker.ninja/) - CIO at Capitec Bank, South Africa. |
0 commit comments