WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Example post

<p>Lorem ipsum dolor sit amet</p>
<p>Morbi elementum odio vel tortor adipiscing vel tempor risus ullamcorper.</p>

What I have done :

function first_paragraph($content){
    return preg_replace('/<p([^>]+)?>/', '<p$1 class="first-paragraph">', $content, 1);
add_filter('the_content', 'first_paragraph');

Which will have different class for first paragraph

Now I want add <span class="first-letter">L</span> and result should be like

<p class="first-paragraph"><span class="first-letter">L</span>orem ipsum dolor sit amet</p>
<p>Morbi elementum odio vel tortor adipiscing vel tempor risus ullamcorper.</p>

Let me know


I got it worked but not a perfect solution.

function first_paragraph($content){
    $content = substr_replace($content, '</span>', 4, 0);
    $content = substr_replace($content, '<span class="first-letter">', 3, 0);
    $content = preg_replace('/<p([^>]+)?>/', '<p$1 class="first-paragraph">', $content, 1); 
return $content;
add_filter('the_content', 'first_paragraph');
share|improve this question
is there any reason you don't want to use the :first-letter css selector? Don't tell me you need this working in IE 6 too... – onetrickpony Jan 23 '11 at 8:14
CSS can do it for you, and like said above, unless you need to specifically have this code work for older browsers that don't support the :first-letter selector, the CSS approach would probably be better/easier(and faster than performing regex). – t31os Jan 23 '11 at 9:17
@One Trick Pony need working on all browsers – haha Jan 23 '11 at 10:05
up vote 1 down vote accepted

This drop caps plugin does what you are looking for: http://wordpress.org/extend/plugins/drop-caps/

If you're trying to do drop caps this should be enough. If you want to do more you could look into the code and see how the plugin wraps the 1st char in a span. It probably uses preg replace too. Id look myself but I'm answering this on my phone and have no way of looking at the code.

share|improve this answer
Thanks matt for the drop caps. – haha Jan 23 '11 at 10:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.