Webaholic - Blog


WordPress PRE und CODE Shortcodes

Read in english

Da man im Visuellen Editor in WordPress zwar inzwischen wunderbar code schreiben kann - denn der wird automatisch in HTML entities umgewandelt - aber leider nicht so ohne weiteres code-Tags schreiben kann (die werden ja mit umgewandelt) habe ich mal ein Plugin geschrieben, welches

[code att="wert"]// mein Code[/code]

automatisch in

<code att="wert">//mein Code</code>

umwandelt. Das gleiche gilt auch für

[pre att="wert"]...[/pre]

Dabei können unbegrenzt viele Attribute angegeben werden.

Verschachteln ist möglich

[pre][code]...[/code][/pre]

nur als Beispiel, es können aber auch alle anderen (verfügbaren) Shortcodes verwendet werden.

UPDATE: 05.05.2008 23:00

Version 1.1:

Neu hinzugekommen:

  • Syntax Highlighting - einfach [code hl=”sprache”] oder [pre hl=”sprache”] verwenden
  • Zeilennummerierung - einfach [code ln=”"] oder [pre ln=”"] verwenden
  • Der Shortcode [nsc] mit dem es möglich ist shortcodes ins Posts zu verwenden ohne dass diese ausgeführt werden - z.B. weil man über seine Shortcodes berichten will und ein Beispiel für deren Verwendung geben will…
  • Die Anführungszeichen (sowohl doppelt als auch einfach) werden von wordpress automatisch in typografische Anführungszeichen umgewandelt - das macht sich im Code aber nicht so gut, desswegen werden die in zukunft innerhalb der Shortcodes wieder zurück in die einfachen bzw. doppelten Anführungszeichen umgewandelt.

UPDATE 07.05.2008 01:06

Version 1.2

Verbesserter schutz vor der Konvertierung von Zeichenketten die wordpress automatisch umwandelt (wie z.B. die diversen Anführungszeichen, das doppelte Minus etc.)

Einfach hier runterladen. Lizenz: GPL

— —
English
While there are no problems publishing code in wordpress anymore since you can easily add it to your posts in visual editor mode and it will automatically be escaped there are problems if you want to wrap it in <code></code> since this tags will be escaped too.

I wrote a plugin to solve this problem using the new shortcodes in WP 2.5.

It will do the following:

[code attr="value"] //code [/code]

will be translated to

<code attr="value"> //code </code>
[pre attr="value"] // code [/code]

will be translated to

<pre attr="value"> // code </pre>

while you may use as many attributes as you want to.

and it can be nested

[pre][code] //code [/code][/pre]

for example but every shortcode will work inside the content

UPDATE: 05.05.2008 23:00

Version 1.1:

New:

  • Syntax Highlighting (useage: [code hl=”language”] or [pre hl=”language”])
  • Line Numbering (using ol) (useage: [code ln=”"] or [pre ln=”"])
  • Added the shortcode [nsc] with whom you can write about your shortcodes without them beeing executed - this is great if you want to give some explanations about how to use them. Just wrap your Shortcodes in [nsc][/nsc] and they will not be executed anymore.
  • Doublequotes and Singlequotes are converted into curly quotes by wordpress when used inside the WYSIWYG Editor. Since this is no good solution for codeexamples they will be resetted to be normal double and single quotes again when they are within [code] or [pre] shortcode.

UPDATE 07.05.2008 01:06

Version 1.2

Changes: Your codes will be better pretected from being converted by wordpress whenever they are wrapped by one of the shortcodes or by the real <code> and <pre> elements.

Grab it here - its licenced GPL.

Einen Kommentar schreiben

 

Abonnieren ohne einen Kommentar abzugeben