Displaying PHP-Code in a post (8 posts)

  1. Jerome
    Posted 12 years ago #

    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

  2. Jerome
    Posted 12 years ago #

    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 ;).

  3. Alex King
    Posted 12 years ago #

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

  4. Dougal Campbell
    Posted 12 years ago #

    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.

  5. Dougal Campbell
    Posted 12 years ago #

    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 :)

  6. Mike Little
    Posted 12 years ago #

    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,

  7. Matt Mullenweg
    Posted 12 years ago #

    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...

  8. Dougal Campbell
    Posted 12 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.