When I post a PHP code snippet into a wordpress post it will convert

<?PHP into <!--?PHP

To avoid this I must replace <?PHP with &lt;?PHP

I am asking if there is a setting or easy method to avoid this? If not I am thinking of maybe writing a PHP function that will search the content filed and replace <?PHP with &lt;?PHP

Any ideas

2 Answers 2


PHP is filtered out of the WordPress post editor as a security feature. There are some plugins that let you put them in, but I think they're a pretty bad idea, so I won't even link to them.

If you need PHP on a page, you should be putting that code in a template file or in functions.php or a plugin file that hooks to a page you want to modify.

  • Hello sorry I wasn't clear, I am posting code for my syntax highlighter to show the code on the screen, not run the code. I used to use a syntax plugin but now I am using my own. Maybe my old syntax highlighter filtered this for me and I didn't realize it
    – JasonDavis
    Aug 26, 2012 at 23:44
  • Aha. I understand. I suspect you're right that this is getting filtered out. I like Syntax Highlighter Evolved from Automattic. (wordpress.org/extend/plugins/syntaxhighlighter)
    – mrwweb
    Aug 26, 2012 at 23:57
  • That is the plugin I have always used but I just switch to the new Rainbow.js its more lightweight and better IMO craig.is/making/rainbows
    – JasonDavis
    Aug 27, 2012 at 0:01
  • If you take a look at Syntax Highlighter (plugins.trac.wordpress.org/browser/syntaxhighlighter/tags/3.1.2/…) evolved you'll see that it's way more than just a JS highlighting library. I don't see why you'd want to reinvent the wheel on this one.
    – mrwweb
    Aug 27, 2012 at 0:44
  • That plugin is simply a wrapper for Alex Gorbatchev JS highlighter, the only "other" stuff it does is detect which brush files to load and a few other minor things... It's a great library but it's a bit outdated and better JS highlighters are on the market now like Rainbow.js much better performace, also this new one Prism from Lea Verou. I't not re-inventing the wheel its taking an old sluggish library and replacing with a new fast one
    – JasonDavis
    Aug 27, 2012 at 10:20

Here is my only idea so far

function strip_php($content) {
    return str_ireplace("<?PHP", "&lt;?PHP", $content  );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.