views:

25

answers:

1

Hello,

I'm creating a new WP theme and I would like to allow the user to insert a divider in between paragraphs or images he/she is entering, for a post/page.

I want the output to be something like:

<div class="divider"></div>

But I don't want the user to have to enter HTML in the WYSIWYG editor. Is it possible to ask them to enter something like:

<-- break -->

and then translate that to the div markup on display?

Thanks.

+2  A: 

Build a function in your theme's functions.php file like this:

function add_div( $content ) {

   $content = str_replace( '<!-- break -->', '<div class="divider"></div>', $content );
   return $content;

}

then add the following to the theme:

add_filter( "the_content", "add_div" );

The function uses PHP's string replace function to find the text you want your users to input and replace it with the text you want to render, the add_filter() function uses Wordpress's content filter to apply your function to the content of each post after it is read from the database, but before it is rendered to the browser.

This will work in PHP4 and up, which is still the official level of support for Wordpress.

cori
worked like a charm. Many thanks.
givp