Displaying PHP Code on your pages.

This page is still being updated so some of the information may be incomplete.

The problem with displaying the actual code on pages as opposed to executing the Code, on a site were we have enabled the plugin to execute php on our pages is – How can WordPress tell the difference?

Herein lies our Dilemma , but as with any dilemma there is always a solution. So let’s take a moment to investigate.
In the case where we have the plugin – Exec-PHP that executes any code in your posts, pages and text widgets.
[Reference : Version 4.9 | By Sören Weber | Visit plugin site]

So let’s try out Frank Verhoeven’s FV Code Highlighter

The PHP code needs to be encapsulated with the following tags { code type=php } Your PHP Code here { /code }. NOTE: I have added spaces after the { and before the } in the above snippet so it will display. Removing the spaces I inserted will result in the following…

{code type=php} Your PHP Code here {/code}

Which is what we are after…

So let’s see what happens with the above PHP code.

The following PHP code was typed in under the “Visual” Tab.
{code type=php}
<?php

echo “hello world”;

?>
{/code}
Doing the same under the “HTML” Tab results in…
{code type=php}

echo "hello world";

?>
{/code}
Which gives us our CSS formatted output with the words – hello world. So the PHP code has been executed.Which is what I expected, so keep that little tidbit under your cap.

BUT if I edit the page/post under the Visual Tab and perform an update, the PHP code is lost between those last tags, Hmmm! So stick to using the Visual Tab. So while not perfect, it’s a start.

My own preference in achieving this.

Personally, I’d rather have the Code already styled and in a separate file and include it.
That has it’s own issues.
There is always more than one way to skin a WordPress site, but the method you end up using is up to you.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>