logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$5
What is wrong with prettify?

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:

http://webbhjalp.se/wordpress-inlagg-kolumner/

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

If I'm manually changing <div to &lt;div 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.

This question has been answered.

Jens Filipsson | 01/12/12 at 11:25am Edit


(4) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    01/12/12
    11:48am
    Francisco Javier Carazo Gil says:

    Hi Jens,

    Maybe some filter is doing it. Try to unable all them:

    add_action( 'all', create_function( '', 'var_dump( current_filter() );' ) );

    • 01/12/12 11:49am

      Francisco Javier Carazo Gil says:

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

    • 01/12/12 11:50am

      Jens Filipsson says:

      Your function broke everything....

    • 01/12/12 11:59am

      Francisco Javier Carazo Gil says:

      Jens,

      From: 5 ways to debug WordPress


      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.


      I have to go out, later I continue.

    • 01/12/12 12:50pm

      Francisco Javier Carazo Gil says:

      Look at it: WordPress: Disable Auto Paragraphing, wpautop() and clean_pre() Functions

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

    • 01/12/12 3:02pm

      Jens Filipsson says:

      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!

  • avatar
    Last edited:
    01/12/12
    11:59am
    Julius J. says:

    Ummm, yesterday in shortcodes I solved similar problem with

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

    • 01/12/12 12:03pm

      Jens Filipsson says:

      How do I add that to my function then?

    • 01/12/12 12:12pm

      Julius J. says:

      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.

    • 01/12/12 12:15pm

      Jens Filipsson says:

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

  • avatar
    Last edited:
    01/12/12
    2:49pm
    She Codes says:

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

    • 01/12/12 2:56pm

      Jens Filipsson says:

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

    • 01/12/12 3:31pm

      She Codes says:

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

    • 01/12/12 3:49pm

      Jens Filipsson says:

      No difference I'm afraid :(

    • 01/12/12 4:12pm

      She Codes says:

      One last idea from me:

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

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

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

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

    • 01/12/12 4:15pm

      She Codes says:

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

    • 01/12/12 4:21pm

      Jens Filipsson says:

      Thing is, my function replaces < with &lt;

      but without it, nothing at all displays...

    • 01/12/12 4:37pm

      She Codes says:

      Can you lose the

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

    • 01/12/12 4:53pm

      She Codes says:

      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.

    • 01/12/12 4:59pm

      She Codes says:

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

    • 01/12/12 5:02pm

      Jens Filipsson says:

      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?

    • 01/12/12 5:27pm

      She Codes says:

      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.

    • 01/12/12 5:31pm

      Jens Filipsson says:

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

  • avatar
    Last edited:
    01/12/12
    3:39pm
    Julio Potier says:

    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.

    • 01/12/12 3:44pm

      Jens Filipsson says:

      The result:

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

    • 01/12/12 3:49pm

      Julio Potier says:

      Can you paste on pastebin.com and email me the original code you paste in the shortcode ? Thank you
      mail : juliobosk@gmail.com
      And pastebin.com

      See you soon

This question has expired.



Jens Filipsson, Julio Potier, Francisco Javier Carazo Gil voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.