How to insert lines of example code into a Wordpress post

Whenever it is a long time I inserted some code in a post in this WordPress blog, when I have to do it again I do the same mistake … so it is worth to write a post for me and may be for you too!! ūüėČ

The possible language (or lang) parameter controls are supported, as stated in this WordPress post:

  • actionscript3
  • bash
  • clojure
  • coldfusion
  • cpp
  • csharp
  • css
  • delphi
  • diff
  • erlang
  • fsharp
  • go
  • groovy
  • html
  • java
  • javafx
  • javascript
  • latex (you can also render LaTeX)
  • matlab (keywords only)
  • objc
  • perl
  • php
  • powershell
  • python
  • r
  • ruby
  • scala
  • sql
  • text
  • vb
  • xml

We both know that there are some strings (e.g. [sourcecode language=”csharp”] Here your C#¬†code [/sourcecode]) you have to add before and after the lines of code you want to add in your page in order to have it well indented properly … BUT, if it does not work properly when you see the page, what we have to remember too is to check in the Text tab if some <span> were automatically added by the editor and, in that case, correct command strings (and possibly also the code!)¬†being in the Text tab (and not in the Visual one) … otherwise that escaped command strings will be shown as a simple lines of text in the page, as it is in the following:

[sourcecode language=”csharp”] Here your C# code [/sourcecode]

instead of what you would like, that is:

 Here your C# code 

Even adding some formatting feature like, for example, Italicizing some text (as in the following), can break the command feature you added and the related tag will be shown (e.g. the <em></em> in the case of Italicization):

 <em>int Here your code</em> 

WRONG WAY: better to write the commands and the code when you are in the Text tab

WRONG WAY: if you go to the Text tab you could see that commands were possibly escaped and the code (you possibly paste) contains a lot of not useful <span> tags.

RIGHT WAY: commands and code are written when you are in the Text tab!

If you write the code correctly from the Text tab, even simply going to the Visual tab you can already see the code inside a grayed box (saying that you did the insertion in the right way!!), even thought the commands line are still visible and only seeing the page will give you the final view formatting.

RIGHT WAY: going to the Visual tab you can already see the code inside a grayed box

RIGHT WAY: in the Visual tab, if you see the code inside a grayed box, it means that is well formed

RIGHT WAY: when you see the page, the code is properly indented and the commands string are hidden

RIGHT WAY: when you finally see the page, the code is properly indented and the commands strings are not shown anymore

Informazioni su Enzo Contini

Electronic engineer
Questa voce è stata pubblicata in Just for fun!, Review e test. Contrassegna il permalink.

Lascia un Commento/Leave a comment

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

This site uses Akismet to reduce spam. Learn how your comment data is processed.