开发者

Adding custom tags in Wordpress

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜