So you want to turn this int x = 137;
into the following on your WordPress site?
int x=137;
Okay. But first here’s a couple of links full of additional information –
This is what WordPress has to say about Writing Code In Your Posts
If you want the prettifier javascript code complete with implementation instructions
However, whether you peruse the above links or not, here’s a quick way to do it…
Login to your WordPress site and navigate to –>Appearance–>Editor and locate the header.php file.
Add to the header.php file the following line of code between the html tags <head>...</head>
– this gets the javascript library files online!
<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js?skin=sunburst"></script>
Then in the post editor select the visual tab to make sure you are in the visual editor and enter your code. This helps make sure the code is not interpreted as code by the browser.
By default, the way a piece of code written or pasted to WordPress post editor is interpreted depends on whether you use visual or HTML post editor. Visual editor will consider the code to be an ordinary text, converting (encoding) the < and > characters into their < and > character entity equivalents, so that the code is not interpreted by a web browser. Quotes are converted too, but remember that by default, WordPress also applies auto-correction so that the text is quoted properly according to your language specifics. HTML editor does not convert any of these characters, so be aware that HTML and CSS markup you use in your code examples will be recognized by a web browser and you may end up with a funky looking text and a messed up layout.
Next select the text tab to change to the HTML editor and wrap the code-text with these tags
<pre class="prettyprint">...</pre>
That’s it! – Now you can publish your code like a professional.
For example here’s a function that tests if a number is prime or not – ‘complete with syntax highlighting’ 🙂
bool isCandidatePrime (unsigned long candidate) { bool isPrime = false; unsigned long num = candidate; //printf("Commencing Prime Factorisation... %lu\n\n", num); unsigned long squareLimit = sqrtl(num); // set known prime sequence array int primeSequenceArray[] = {2,4,2,2}; // set prime sequence array index to 3 int primeSeqIndex = 3; // we'll need an incrementor - use an arbitrary non logical value int incrementor = 1; // we'll also need to keep a running total of prime factors int numPrimeFactors = 1; // i represents the current factorizer starting at 2 // (num / 2)+1 for (unsigned long i = 2; i <= squareLimit; i++) { if (num % i == 0) { // num is not prime and i is a prime factor of num //printf("%lu\n", i); numPrimeFactors++; num /= i;// i--;// set i to last known factor - 1 } // this additional routine reduces computational time by approx 33.3% if (i > 7) { squareLimit = sqrtl(num); for (i = 11 ; i <= squareLimit ; i += incrementor ) { if (num % i == 0) { // num is not prime and i is a prime factor of num //printf("%lu\n", i); numPrimeFactors++; num /= i; // dont increment i and test again incrementor = 0; // calculate new square root limit squareLimit = sqrtl(num); squareLimit++; } else { primeSeqIndex++; primeSeqIndex %= 4; incrementor = primeSequenceArray[primeSeqIndex]; } } } } //printf("%lu\n\n", num); //printf("number of prime factors is %i\n\n", numPrimeFactors); if (numPrimeFactors == 1 && candidate > 1) { isPrime = true; //printf("%lu IS A PRIME NUMBER\n\n", candidate); } //else { printf("%lu IS NOT A PRIME NUMBER\n\n", candidate); } //printf("END OF ROUTINE\n\n"); return isPrime; }