Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

What is wrong with prettify? WordPress

  • SOLVED

I'm using prettify on my blog to be able to paste code.

In functions.php I have the following code to make the code clean (the < code> tags without spaces off course):

function bbcode( $attr, $content = null ) {
$content = clean_pre($content); // Clean pre-tags
return '<pre class="prettyprint">< code>' . str_replace('<', '&lt;', $content) . '< /code></pre>';
}
add_shortcode('kod', 'bbcode');


So when I'm pasting code in my posts, I'm doing inside a shortcode, like this:

[kod]Some code[/kod]

So far, so good, and prettify works pretty well, accept that sometimes I'm getting weird line-breaks.

When pasting this code:

<?php if ($col == 1) echo '<div class="rad">'; ?>

This is what I'm getting:

<?php if ($col == 1) echo '
<div class="rad">'; ?>


Can someone point me in the right direction to why this is happening? You can see an example post here:

[[LINK href="http://webbhjalp.se/wordpress-inlagg-kolumner/"]]http://webbhjalp.se/wordpress-inlagg-kolumner/[[/LINK]]

Notice that on all code examples where I'm trying to echo a <div> I'm getting line breaks.

If I'm manually changing <em><div</em> to <em>&lt;div</em> it works as it should, but the function I'm using is also doing this, so I can't understand what's happening!

Thanks!

// Jens.

Answers (4)

2012-01-12

She Codes answers:

<span class="str">' &lt;div class="rad"&gt;'</span>

This is the source code from your site.
There is no break, there is a space there - between the ' and &lt;

Are you sure this space character does not come from your function or from the text in the post?


Jens Filipsson comments:

Yes, I saw that as well! It's weird, but it still shouldn't force a line break I reckon?


She Codes comments:

This is by no means the best way to do this, but it is the quickest way I could think of to check if clean_pre is adding the break. Please try this instead of your function (the < code> tags without spaces off course):

function bbcode( $attr, $content = null ) {
$content = str_replace('<', '&lt;', $content);
$content = str_replace('>', '&gt;', $content);
$content = str_replace('&lt;p&gt;', '<p>', $content);
$content = str_replace('&lt;/p&gt;', '</p>', $content);
$content = str_replace('&lt;br /&gt;', '<br />', $content);

$content = clean_pre($content); // Clean pre-tags
return '<pre class="prettyprint">< code>' . $content . '< /code></pre>';
}

add_shortcode('kod', 'bbcode');


Jens Filipsson comments:

No difference I'm afraid :(


She Codes comments:

One last idea from me:

function bbcode( $attr, $content = null ) {

$content = clean_pre($content); // Clean pre-tags

return '<pre class="prettyprint"><code>' . $content . '< /code></pre>';

}
add_shortcode('kod', 'bbcode');


She Codes comments:

Sorry:

function bbcode( $attr, $content = null ) {

$content = clean_pre($content); // Clean pre-tags

return '<pre class="prettyprint">< code>' . $content . '< /code></pre>';

}
add_shortcode('kod', 'bbcode');


Jens Filipsson comments:

Thing is, my function replaces < with &lt;

but without it, nothing at all displays...


She Codes comments:

Can you lose the <pre> tag and style the <code class="prettyprint"> the same way?


She Codes comments:

No, you cannot... Forget the last one.

I think I found where the problem comes from.

1. Open brand new post, paste this
'<div></div>
in Visual editor, then change to HTML.


2. Delete everything and now paste it in HTML, change to Visual and then change back to HTML.

I think the HTML editor does this, because it forces DIV (block-level) tags on a new line. I suppose the same would happen if the tag was H2, but not if it was SPAN.


She Codes comments:

Therefore, if you don't switch between both editors, the problem should not occur.

Or, if you go to HTML, delete the space and then re-save the post whilst still in HTML view.

No other ideas...


Jens Filipsson comments:

That's totally the problem. I'm usually just using the html editor though. I never change to visual. So I really shouldn't have the problem in the first place, right?

Even if I save it the way it looks right in HTML, it still comes out wrong as it seems. Is it possible to override this somehow?


She Codes comments:

If you go to Visual editor and re-paste your shortcode there:

[kod]<?php if ($col == 1) echo '<div class="rad">'; ?>[/kod]

in html this should be converted to

[kod]&lt;?php if ($col == 1) echo '&lt;div class="rad"&gt;'; ?&gt;[/kod]

and maybe the div tag would not be recognized as such, therefore would not break on a new line.

This is not a real solution though, as you would have to perform it on a case per case basis.


Jens Filipsson comments:

Yes, this is probably the solution I have to go with. Very annoying though, I hate not finding the answers to things! :)

2012-01-12

Francisco Javier Carazo Gil answers:

Hi Jens,

Maybe some filter is doing it. Try to unable all them:
add_action( 'all', create_function( '', 'var_dump( current_filter() );' ) );


Francisco Javier Carazo Gil comments:

If there's no problem with other tags, something has to be in the middle.


Jens Filipsson comments:

Your function broke everything....


Francisco Javier Carazo Gil comments:

Jens,

From: [[LINK href="http://nacin.com/2010/04/23/5-ways-to-debug-wordpress/"]]5 ways to debug WordPress[[/LINK]]


<em>There’s an ‘all’ hook that fires for all actions and filters. Example usage:

add_action( 'all', create_function( '', 'var_dump( current_filter() );' ) );
You’ll be surprised how many hooks get executed on every page load. Good for troubleshooting and identifying the right hook.

There’s also a ‘shutdown’ hook you can use in combination with, say, SAVEQUERIES, and write the query information to the database. It’s the last hook to run.
</em>

I have to go out, later I continue.


Francisco Javier Carazo Gil comments:

Look at it: [[LINK href="http://emrahgunduz.com/categories/development/wordpress/wordpress-disable-auto-paragraphing/"]]WordPress: Disable Auto Paragraphing, wpautop() and clean_pre() Functions[[/LINK]]

It seems that clean_pre() is introduce autoparagraph. With this code you can solve it.


Jens Filipsson comments:

Unfortunately that didn't work. And looking at the generated source, there are no p or br tags either... Only weird thing is a single space (sp ace), which I can't find where it's coming from!

2012-01-12

Julius J. answers:

Ummm, yesterday in shortcodes I solved similar problem with

$content = str_replace('<br />', '', $content);


Jens Filipsson comments:

How do I add that to my function then?


Julius J. comments:

function bbcode( $attr, $content = null ) {

$content = str_replace('<br />', '', $content);
$content = clean_pre($content); // Clean pre-tags

return '<pre class="prettyprint">< code>' . str_replace('<', '&lt;', $content) . '< /code></pre>';

}

add_shortcode('kod', 'bbcode');


Simple as that.


Jens Filipsson comments:

Didn't work unfortunately, no and no br tags there what I can see...

2012-01-12

Julio Potier answers:

Hello try this :

function bbcode( $attr, $content = null ) {
return '<pre class="prettyprint">< code>' . str_replace('<', '&lt;', $content) . '< /code></pre>';
}
add_shortcode('kod', 'bbcode');


I deleted the clean_pre()
Paste me the result please thank you.


Jens Filipsson comments:

The result:

<?php if ($col == 1) echo "
<div class="rad">"; ?><br />
<!-- Om vi är på kolumn 1, lägg till en ny rad-container -->


Julio Potier comments:

Can you paste on pastebin.com and email me the original code you paste in the shortcode ? Thank you
mail : [email protected]
And pastebin.com

See you soon