Sample
|
1 2 3 4 5 6 7 8 |
// A sample class class Human { private int age = 0; public void birthday() { age++; print('Happy Birthday!'); } } |
Using the Tag Editor
The Tag Editor makes adding code, changing settings and all that much easier with a simple dialog. I’d recommend it over the manual approach, since you can fine-tune the Crayon after the editor generates it
Pre-formatted Tags
You can also use <pre class="attributes" title="something">...</pre> so that:
- Disabling Crayon still presents the code as pre-formatted text and is readable
- Enforces proper encoding of entities (< in HTML view and not <)
- The code snippets are reusable with other syntax highlighters
The Tag Editor generates tags like these. Attributes names are separated from values by either : (default) or _.
E.g. <pre class="lang:php theme:twilight mark:1,2-4" title="something">...</pre>
You can optionally provide the following attributes:
| Attribute | Description | Example |
| lang | Specify a Language ID, these are the folders in the langs directory and appears in the list of Languages in Settings. |
lang="java" |
| url | Load a file from the web or a local path. You give a relative local path instead of absolute (see Files). For languages with defined extensions (see Languages in Settings) you don’t even need to provide the lang attribute, as this will be detected if your file ends with it, as in the example. |
url="http://example.com/code.java"url="java/code.java" |
| title | Give a title for your code snippet. Appears in the toolbar. | title="Sample" |
| mark | Mark some lines as important so they appear highlighted. You can specify single numbers, comma separted, a range, or a combination. | mark="5-10,12" |
| range | Specify the range of lines from the input code to use in the output. You can specify a single number or a single range. | range="3-5", range="3" |
Mixed Highlighting
You can even mix code together like on a real HTML page, by having <script>, <style> and <?php...?> tags all in a single Crayon and setting the language to HTML. Find out more here.
Languages
Language information is found here in the WordPress Admin:
Settings > Crayon > Languages > Show Languages
You can customize and create new languages and define how to capture each element (keywords, comments, strings, etc.) with regular expressions. Languages are structured /langs/lang-name/lang-name.txt. Take a look at /langs/default/default.txt and check out the neat regex of the default/generic language. I’ve also spent considerable time putting together a quick guide in /langs/readme.txt. If your language is missing, send me an email. There will be more languages added over time, of course.
Themes
Crayon comes with built-in Themes to style your code. See a sample of the current set of themes. Themes are structured /themes/theme-name/theme-name.css. If you know CSS, take a look at /themes/default/default.css to get an idea of how they work and how you can change/create them. The specification for CSS classes is here.
Comments
Then enable these settings in WordPress Admin > Settings > Crayon:
- “Display the Tag Editor in any TinyMCE instances on the frontend (e.g. bbPress)”
- “Allow Crayons inside comments”
Before Crayon 2.1.2 you needed to add the following manually at the end of your theme’s functions.php file to enable TinyMCE in the comment box. This requires at least version 3.3 of WordPress.
|
1 2 3 4 5 6 7 |
add_filter('comment_form_defaults', 'tinymce_comment_enable'); function tinymce_comment_enable ( $args ) { ob_start(); wp_editor('', 'comment', array('tinymce')); $args['comment_field'] = ob_get_clean(); return $args; } |
Internationalization
Crayon comes translated in several languages already, and if yours is included it will translate Crayon given you’ve followed instructions about how to show WordPress in Your Language. If Crayon doesn’t support your language or you’d like to help improve it:
- Recommended Method
- Download POEdit. Add
..to the source paths under catalog preferences, andcrayon__,crayon_nandcrayon_eunder source keywords. - Update and start making translations.
- Send the
.pofile to me at crayon.syntax@gmail.com and give me your name and a URL for credit.
- Download POEdit. Add
- Alternative Method
- Download Codestyling Localization.
- Go to Tools > Localization and click rescan on Crayon, then add/edit a language
- From the edit screen you can edit all translatable strings
- Send the
.pofile to me at crayon.syntax@gmail.com and give me your name and a URL for credit
Fonts
You can define fonts and font-sizes within Themes, but you can also override the theme’s font with those inside /fonts/ and also provide @font-face fonts just like in themes – it’s just CSS after all. You can also include Google Web Fonts.
Disable Highlighting
You can temporarily disable highlighting for a piece of code using the highlight="false" attribute. You can also prevent Crayon from touching any kind of tag by using the crayon="false" attribute. If using pre tags, put crayon:false in the class.
Minification
Since 2.2.0 Crayon’s JavaScript and CSS resources are minified into single files. Previous versions depended on the W3 Total Cache plugin to minify your CSS and JavaScript.
Use in Other Environments
If you’re planning to use Crayon somewhere other than WordPress: Using Crayon Manually in PHP
Other Uses in WordPress
If you want to show Crayon in places other than posts and comments in WordPress: Adding Crayon To Posts In WordPress Programmatically
Settings
Crayon is versatile so you can override global settings for individual Crayons with attributes. The Tag Editor lets you do all this using a simple dialog!
| Setting | Allowed Value | Description |
| theme | string | ID of the theme to use |
| font | string | ID of the font to use |
| font-size | number > 0 | Custom font size in pixels |
| min-height/max-height | number > 0 followed by px/% | Minimum/maximum height in units |
| height | number > 0 followed by px/% | Height in units |
| min-width/max-width | number > 0 followed by px/% | Minimum/maximum width in units |
| width | number > 0 followed by px/% | Width in units |
| toolbar | true/false/”always” | Show or hide the toolbar. “always” shows always (without mouseover). |
| top-margin | number >= 0 | Top margin in pixels |
| bottom-margin | number >= 0 | Bottom margin in pixels |
| left-margin | number >= 0 | Left margin in pixels |
| right-margin | number >= 0 | Right margin in pixels |
| h-align | “none/left/right/center” | Horizontal alignment |
| float-enable | true/false | Allow floating elements to surround Crayon |
| toolbar-overlay | true/false | Overlay the toolbar on code rather than push it down when possible |
| toolbar-hide | true/false | Toggle the toolbar on single click when it is overlayed |
| toolbar-delay | true/false | Delay hiding the toolbar on MouseOut |
| show-title | true/false | Display the title when provided |
| show-lang | “found/always/never” | When to display the language. |
| striped | true/false | Display striped code lines |
| marking | true/false | Enable line marking for important lines |
| nums | true/false | Display line numbers by default |
| nums-toggle | true/false | Enable line number toggling |
| plain | true/false | Enable plain code. Disabling will also disable plain toggling and copy/paste which use the plain code. |
| crayon | false | Prevent from turning the tag into a Crayon. |
| highlight | true/false | Prevent from highlighting the code, treats it as plain text instead. |
| plain-toggle | true/false | Enable plain code toggling |
| show-plain-default | true/false | Show the plain code by default instead of the highlighted code |
| copy | true/false | Enable code copy/paste |
| popup | true/false | Enable opening code in a window |
| scroll | true/false | Show scrollbar on mouseover |
| tab-size | number >= 0 | Tab size |
| trim-whitespace | true/false | Trim the whitespace around the code |
| mixed | true/false | Enable mixed highlighting (multiple languages in code) |
| show_mixed | true/false | Show the mixed highlighting plus sign |
| start-line | number >= 0 | When to start line numbers from |
| fallback-lang | string | ID of the language to use when none is detected |
| local-path | string | Local path to load file from |
| touchscreen | true/false | Disable mouse gestures for touchscreen devices (eg. MouseOver) |
| disable-anim | true/false | Disable animations |
| runtime | true/false | Disable runtime stats |
| error-log | true/false | Log errors for individual Crayons |
| error-log-sys | true/false | Log system-wide errors |
| error-msg-show | true/false | Display custom message for errors |
| error-msg | string | The error message to show for errors |
| mixed | true/false | Allow mixed languages using delimiters and tags |
Legacy Tags
Using the legacy tag [crayon attributes] some code [/crayon] is possible but I highly recommend using <pre> tags instead. You can also convert legacy tags automatically.
Mini Tags
You can use Mini Tags but they are still legacy tags and are depreciated.
Troubleshooting
Why are the controls not working?
Make sure jQuery is included in your theme ONCE and before all its dependents. Make sure you use wp_register_script instead of just printing your own <script> tag, otherwise the jquery dependency will be missing and WordPress won’t load any of the Crayon scripts which require it. If you want to change jQuery to your own version, do it like this:
|
1 2 3 4 |
// Remove existing jQuery wp_deregister_script('jquery'); // Replace with your own wp_register_script('jquery', 'url/of/your/jquery.js'); |
Failing to register will not only fail to load Crayon’s scripts, but any other plugin smart enough to use the version of jQuery that is included with WordPress.
Why am I only seeing [crayon-3ffr7fa34a321/]?
Your theme is being naughty. Find where it prints the content/excerpt on that page in the PHP and make sure to add this line before printing, where $output contains your content:
$output = apply_filters('the_content', $output);
This ensures that the content can be filtered by plugins, such as Crayon. Otherwise, Crayon detects and creates the code but can’t replace those funny looking tags in the content.

Pingback: epicgeeks.netgaming, development, and life. -
Pingback: Wordpress: How to Post Source Code | Kampung Toys
Pingback: Правила оформления write-up’ов | PeterPen::Blog
Pingback: 進捗 0106 | bgk devlog
Pingback: Crayon Global Settings
Pingback: Wordpress pluginy | Webovník
Pingback: Order Does Matter: jQuery conflict solved by reordering
Pingback: Quotidiana, la autopsia I : Ncasolajimenez
Pingback: [WordPress] Crayon Syntax Highlighter « MiNu Blog
Pingback: Projekt Soundboard | shell 42
Pingback: Wordpress上のコードをハイライト表示するプラグインを導入(Crayon Syntax Highlighter) | gggrf.com
Pingback: 世界,你好! | 飞熊在天
Pingback: Bila Bercakap Soal Syntax Highlighter | #CodeBase
Pingback: Как красиво вставить программный код в Wordpress. Подсветка синтаксиса. | Мастерская блоггера. Полезные инструменты и информация.
Pingback: 代码高亮插件Crayon Syntax Highlighter | SEEing is BELIEVING
Pingback: Crayon Syntax Highlighterの使い方 | Design Wood Tree
Pingback: Latest Version of WordPress Installed by curnow.biz
Pingback: Crayon Syntax Highlighter plugin | JS Tips
Pingback: Besseres Syntax-Highlighting in Wordpress | Bjoern's Blog
Pingback: 本站安裝的Wordpree外掛一覽表 - Hareion's手札
Pingback: Sitede Kullandığım WordPress Plugin’ler | Berker Sönmez Personal Website
Pingback: Crayon Syntax Highlighter (and its studly author) | SelfPwnt
Pingback: 代码高亮(插件版) | 末日札记
Pingback: Using Crayon Syntax Highlighter with Delphi | squashbrain
Pingback: » シンタックスハイライト
Pingback: [WP] インストールしたプラグイン | 最近,気になったこと...
Pingback: [WP] Crayon Syntax Highlighter | NaeilStory.net
Pingback: How to Add Code Snippets Samples in Wordpress Posts Without Plugin - Providing latest Information Technology Tips - i-Tech Tips
Pingback: Najlepszy syntax highlighter do WordPress’a | Knowicki Bloguje
Pingback: Changing wallpapers using a shell script | Linux Training & Certification
Pingback: Pythonic SVG’ing « ianscottwilson.com blog
Pingback: WordPress代码高亮插件 | 极客吧-专注于各种IT技术的研究
Pingback: BilDblog.ru » Blog Archiv » Как выводить код в публикациях WordPress?
Pingback: WordPress代码高亮插件推荐 | 极客吧-专注于各种IT技术的研究
Pingback: C-Sharp MessageBox Examples | Zero-AGL
Pingback: Casting .toString();
Pingback: Crayon Syntax Highlighter und .htaccess | Bjoern's Blog
Pingback: Some WordPress Plugins | Chen Shan
Pingback: Resources Loaded in Crayon
Pingback: Customize the comment section of the wordpress | My Technical Blog
Pingback: 2012年版!frasmで使っている「とにかく捗るWordPressプラグイン」48選*frasm
Pingback: How to post source code in WordPress
Pingback: Does my code look big in this? | jimsmart.org
Pingback: コードハイライト | WordPress シングルサイト動作確認
Pingback: Syntax Highlighting « CriticMarkup
Pingback: » crayon </sanity>
Pingback: Необходимые плагины для WordPress | Блог Макса
Pingback: How to use prettify with WordPress ← danjjohnson.com
Pingback: How to post source code on WordPress | JJPro
Pingback: fxpixel – Test Syntax Highlighter