Laravel 3 – Using wpautop as a library

I decided to use the autop function from wordpress core to automatically transform line breaks from textareas into <p>s and <br>s, and use it in my libraries folder

(By the way Laravel doesn’t have any native way of doing this, right? I’d feel really stupid if it did)

Read More

So in myproject/application/libraries/formatting.php I have the following:

class Formatting {

    public function autop($pee, $br = true) {
       [wpautop's code here]
    }
}

And call it as

Formatting::autop($text);

so it returns this error

preg_replace_callback(): Requires argument 2, '_autop_newline_preservation_helper', to be a valid callback

It has to do with the following block

if ( $br ) {
    $pee = preg_replace_callback('/<(script|style).*?</\1>/s', '_autop_newline_preservation_helper', $pee);
    $pee = preg_replace('|(?<!<br />)s*n|', "<br />n", $pee); // optionally make line breaks
    $pee = str_replace('<WPPreserveNewline />', "n", $pee);
}

requiring this function to be there for him

function _autop_newline_preservation_helper( $matches ) {
     return str_replace("n", "<WPPreserveNewline />", $matches[0]);
}

This function is apparently responsible for turning single line breaks into <br> tags, If I remove the if block the code works but only with double line breaks, and I’d like to keep my <br>s

If I simply add this function’s code in the same file as the autop function it returns the same error, I dont know the proper way to include the _autop_newline_preservation_helper function, what should I do?

Related posts

Leave a Reply

1 comment

  1. I think I found the answer for myself.

    I inserted the _autop_newline_preservation_helper function INSIDE the autop public function, and now it works.

    I really don’t know if this is a good idea so if anyone thinks this might lead to a problem, hit me up.