skip to Main Content
Menu

Nl2br for WordPress

Was looking around for a solution on one of my wp installations.. didn’t want to go in hardcore with the wp core..

No matter where i tried applying nl2br() on the template the outcome would be something like the one below and like i said i didn’t want to go into exploration of wp-core..

 

 

 

 

 

 

 

 

 

 

 

 

I searched around and found this in one of the word press support forums. and true enough this was the solution to my problemo.. as you can see below the above now resulted to this :

 

 

 

 

 

 

 

 

 

 

 

 

I only had to add the small snippet below to the functions.php and it worked like a charm.

/*

Plugin Name: nl2br the_content
Description: Apply nl2br() to the_content.
Version: 0.1
*/
add_filter( 'the_content' , 'mh_nl2br_it' );
function mh_nl2br_it( $content ) {
return nl2br( $content );
}

 

Source : http://wordpress.org/support/topic/widely-theme-has-no-paragraphs-in-post

*** Update: made some mods to this function. As is, this function does crazy things to html tables. So as an update.. am posting the same function with the mods below.

 

/*

Plugin Name: nl2br the_content
Description: Apply nl2br() to the_content.
Version: 0.1
*/
add_filter( 'the_content' , 'mh_nl2br_it' );
function mh_nl2br_it( $content ) {

// string to find in $content
$findme   = ‘<table’;

//if string ‘<table’ is found, this assumes that your text precedes your table.
if(strstr($content,$findme))
{

$where= strrpos($content,'<table’);
//separate text from table
$part1=substr ($content,0, $where) ;
$part2=substr ($content,$where);
//join text($part1) with table($part2) subjecting text to nl2br.
$content = nl2br($part1). $part2;
}else{
//subject $content to nl2br
$content=nl2br( $content );
}
// return processed contents for display as post or page content
return  $content;
}

Alphonse

Editorial Staff at ETN Multimedia is led by Alphonse Tan. Page maintained by Alphonse Tan

Back To Top