WP3.0 Displaying PHP Code

glenwheeler

Senior Member
Hi Guys,

I wrote a small blog post yesterday evening where I wanted to display some php code however the post keeps outputting the code and not just as text.

I have used the
Code:
tags but this is pretty damn useless as it does nothing!

Does anyone know of a widget or a tag that will allow me to display code as text in the post.

You can see the post I did is outputting a search box when all i want to display is some php code.

Blog | Glen Wheeler – Freelance Web Design | Web Design Newcastle & North East UK

PS: Excuse the styling, am just playing with the appearance of things at the moment. I just keep updating the posts so I have some decent content when the site is all nice and tidy.

Any help would be cool, cheers dudes and dudette's :clap:
 
you could code a blockquote style in css and then make all your code in blockquotes :)
 
You need to encode your entities lol.

Instead of:

Code:
<pre><code><div class="example">Text</div></code></pre>

you need

Code:
<pre><code><div class="example">Text</div></code></pre>

And don't put code in a blockquote, that's not what they're for. Wrap any encoded code in <pre> and <code> tags.
 
Haha sorry, t'was a tad blunt.

Also, only wrap code in <pre> tags if it needs it, obviously :)
 
Semantics. Plus as soon as you disable styles the code element would 'collapse', proving unreadable.
 
Semantically, the pre tag has no meaning, it modifies appearance, which is something that should be left to CSS. Arguably not for a language where whitespace has real value, but for PHP all it's doing is improving readability.
 
I am not dealing with CSS here I'm talking PHP code that won't display. You can see on the page its creating a search box instead of the code because it's calling the form from wp's default general file as my theme does not use the search-form.php file.
 
Yes, you need to encode your entites. Instead of putting <code><?php blah("blah"); ?></code> you need <code><?php blah("blah"); ?></code>

Bastard thing is making the semi-colon–bracket into a wink, but you get what I mean. Putting unescaped chevrons into markup (through a WYSYWIG or otherwise) will cause a user agent to try parsing the code.
 
Mark Alexander said:
Semantically, the pre tag has no meaning, it modifies appearance, which is something that should be left to CSS. Arguably not for a language where whitespace has real value, but for PHP all it's doing is improving readability.

It provides pre-formatted text, which you require when writing out code that requires whitespace to be fully understood.
The markup is the information, whitespace in code is information, therefore it should be added as such. Adding meaning through CSS (i.e. the indents, line breaks etc) is just wrong.
 
So does the CSS declaration I mentioned. The whitespace has no real value in PHP, it serves only to aid code readability, as would many other depreciated style tags that you wouldn't think of using.
 
Whitespace in coding is not a design feature, or a layout feature, it is a content feature.

The whitespace is part of the content.

Sure it isn't needed. But that makes it "unnecessary" content, not something else.

It's like arguing against using <em> or <strong> they have there uses, and should be used in those situations.
 
You should never think of markup as working-when-used-with-CSS. The two should not have to be combined for something to be readable. Not using <pre> means that, without CSS, the code wouldn't be formatted correctly, the whitespace is content, not style.
 
Renniks said:
Whitespace in coding is not a design feature, or a layout feature, it is a content feature.

Like I said, arguably for some languages where whitespace is important but not PHP - where the code runs the same no matter what tabs, spaces, line breaks or whatever is in between.

Harry said:
You should never think of markup as working-when-used-with-CSS. The two should not have to be combined for something to be readable. Not using <pre> means that, without CSS, the code wouldn't be formatted correctly, the whitespace is content, not style.

But it's perfectly correct without the pre tag.

PHP:
<?php if (1) { echo 'Hello'; } ?>
is just as valid and meaningful as

PHP:
<?php
if (1) {
    echo 'Hello';
}
?>
For the latter you've just changed the format, ie the visual appearance, to make it easier to understand. Not in order to make the content valid or meaningful, because it was already valid and meaningful.

The code retains the same value whether you space it out or turn it bright pink.
 
Yes, the code reads the same, but readability should be added at markup level, not at style level.

The example you use is very handy for your argument, but take:
PHP:
/**
 * Get the week start and end from the datetime or date string from mysql.
 *
 * @since 0.71
 *
 * @param string $mysqlstring Date or datetime field type from mysql.
 * @param int $start_of_week Optional. Start of the week as an integer.
 * @return array Keys are 'start' and 'end'.
 */
function get_weekstartend( $mysqlstring, $start_of_week = '' ) {
	$my = substr( $mysqlstring, 0, 4 ); // Mysql string Year
	$mm = substr( $mysqlstring, 8, 2 ); // Mysql string Month
	$md = substr( $mysqlstring, 5, 2 ); // Mysql string day
	$day = mktime( 0, 0, 0, $md, $mm, $my ); // The timestamp for mysqlstring day.
	$weekday = date( 'w', $day ); // The day of the week from the timestamp
	$i = 86400; // One day
	if( !is_numeric($start_of_week) )
		$start_of_week = get_option( 'start_of_week' );

	if ( $weekday < $start_of_week )
		$weekday = 7 - $start_of_week - $weekday;

	while ( $weekday > $start_of_week ) {
		$weekday = date( 'w', $day );
		if ( $weekday < $start_of_week )
			$weekday = 7 - $start_of_week - $weekday;

		$day -= 86400;
		$i = 0;
	}
	$week['start'] = $day + 86400 - $i;
	$week['end'] = $week['start'] + 604799;
	return $week;
}

which would, without styles, become:

PHP:
/** * Get the week start and end from the datetime or date string from mysql. * * @since 0.71 * * @param string $mysqlstring Date or datetime field type from mysql. * @param int $start_of_week Optional. Start of the week as an integer. * @return array Keys are 'start' and 'end'. */function get_weekstartend( $mysqlstring, $start_of_week = '' ) {$my = substr( $mysqlstring, 0, 4 ); // Mysql string Year$mm = substr( $mysqlstring, 8, 2 ); // Mysql string Month$md = substr( $mysqlstring, 5, 2 ); // Mysql string day$day = mktime( 0, 0, 0, $md, $mm, $my ); // The timestamp for mysqlstring day.$weekday = date( 'w', $day ); // The day of the week from the timestamp$i = 86400; // One dayif( !is_numeric($start_of_week) )$start_of_week = get_option( 'start_of_week' );if ( $weekday < $start_of_week )$weekday = 7 - $start_of_week - $weekday;while ( $weekday > $start_of_week ) {$weekday = date( 'w', $day );if ( $weekday < $start_of_week )$weekday = 7 - $start_of_week - $weekday;$day -= 86400;$i = 0;}$week['start'] = $day + 86400 - $i;$week['end'] = $week['start'] + 604799;return $week;}

Also, just stop and think, you're deleting one element and forcing another to behave just like it—does that not seem a little, well, pointless? <pre> is a valid and non-deprecated element, use it.
 
If you are posting code up to be read, it should be readable.

If it isn't posting to be read, it should be supplied in an external document (in my opinion)
 
I'm not denying that it becomes unwieldy. There are many presentational aspects which, when taken away, would result in unwieldiness. That doesn't stop them being presentational though. Take away CSS and you'll have copy that's horribly more difficult to read than a document with appropriate spacing and line height and line length and all that, but you wouldn't use that as an excuse to ignore the content/presentation separation rule.

Renniks said:
If you are posting code up to be read, it should be readable.

Well bear in mind we're talking about people without CSS here, the vast majority of people are seeing the code as intended if you just use CSS.
 
Back
Top