Support » Fixing WordPress » Displaying PHP-Code in a post

  • Hi!
    I came to WordPress only yesterday, and I am really impressed – and so, I am using it right now ;).
    I want to make a sort of a development blog while programming my own (simple) scripts. As I want to show the progress to the visitors, it would be great if I could display colored PHP-Code in my posts.
    Do you perhaps know a way to do this?
    Thank you very much in advance
    – Jérôme

Viewing 7 replies - 1 through 7 (of 7 total)
  • Okay :o), if nobody wants to help me, I will try to get into WordPress and do it on my own. But don’t blame me, if my blog crashes ;).

    I think I’ve seen some JavaScripts that will do this… can’t remember where though.

    Jerome, colorizing PHP code inside an HTML post is a non-trivial task.
    It would be far easier if you put your code in separate files with a ‘.phps’ extension (if your server supports that), and link to those.
    If you want to get fancy, store the code in external files, then put an <iframe src=”myfile.phps” /> in your post.

    Actually, we’re currently using XHTML 1.0 Transitional, and iframe is valid there.
    About the only other way I can think of to do it would be to bring up the .phps file in your browser, view source, then cut-n-paste that source into your WordPress posting.
    Of course, the PHP source coloring code uses the font tag, so if you’re concerned about maintaining xhtml validity, that’s out, too 🙂

    Hi Jerome,
    it is *much* simpler than that.
    simply call highlight_file(‘your_file_name.php’) which will echo the syntax highlighted source code from the file in place.
    There is a second optional boolean parameter which forces it to return a string rather than echo.
    Of course Dougal’s point about font tags is still valid (or rather invalid for xhtml transitional)
    Hope this helps,

    I’ve been meaning to write something to highlight code in a XHTML/CSS fashion, and I saw in the latest php|architect they have a script that does just that. Great minds…

    Ha, I forgot all about there being a highlight_file() function. Duh 🙂
    Um…. But getting a PHP function call into the middle of a blog post is another matter…. Which is the same reason that I almost suggested (but did not, because my test didn’t work) using [?php include(‘http://mysite/myfile.phps’); ?]
    So I’m thinking that the iframe is still going to be your best bet for right now. It will work, and your main page will still validate.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Displaying PHP-Code in a post’ is closed to new replies.