WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

I'm trying to automate one of our posting aspects. We post a lot of lyrics and have a system of meta boxes which we use to input various pieces of information about each set of lyrics we post. One aspect that is a little annoying is having to search & replace <li></li>tags to the start and end of each new line of lyrics. I was hoping to have PHP echo this automatically but my code is not working at the moment. Here is my untouched code which outputs a verse of lyrics (we use wpautopso that line breaks are respected):

<?php if ( get_post_meta($post->ID, '_cmb_lyrics_1', true) ) : ?>
<?php $lyrics_1 = get_post_meta($post->ID, '_cmb_lyrics_1', true) ?>
<?php echo wpautop( $lyrics_1) ; ?>
<?php endif; ?> 

And here is my attempt at outputting <li>tags, which didn't work. :(

<?php if ( get_post_meta($post->ID, '_cmb_lyrics_1', true) ) : ?>
<?php $lyrics_1 = get_post_meta($post->ID, '_cmb_lyrics_1', true) ?>
echo "<li>\n"; wpautop( $lyrics_1) ; "</li>\n"; ?>
<?php endif; ?>

Any help appreciated. Thanks.

share|improve this question
what exactly is in $lyrics_1? is this multiple lines? – Michael Feb 14 '13 at 11:59
See my answer to the identical question wordpress.stackexchange.com/a/84815/11761 – Max Yudin Feb 14 '13 at 12:44
@MaxYudin. I could not get that to work - returned error unexpected { . – JMB Feb 14 '13 at 21:19


    <?php echo wpautop( $lyrics_1);?>

instead of this line:

<?php echo "<li>\n"; wpautop( $lyrics_1) ; "</li>\n"; ?>

edit: thanks @Toscho for the syntax edit, I made a hasty error in my earlier revision ;-)

share|improve this answer
That doesn't work unfortunately - it was the first thing I tried. It returns like this: <ul> <li><p>Hi<br/> Ok<br/> Let's go<br/> Fo sho</p> </li> </ul> It doesn't apply the <li>to each line, rather before and after each block of lyrics. – JMB Feb 14 '13 at 11:39
do you want to have each p block within li tag, like: <li><p>...</p></li> ? – birgire Feb 14 '13 at 11:58
if so you can try: <?php echo str_replace(array('<p>','</p>'),array('<li><p>','</p></li>'),wpautop( $lyrics_1));?> – birgire Feb 14 '13 at 12:08
This won't work unfortunately. wpautop adds <br /> tags to mark the line end. I changed your search value </p>for <br />and tried \nin place of <p>but to no avail. I got the text returned as one long <li>item! – JMB Feb 14 '13 at 21:23
so you want to change: <ul> <p>Hi<br/> Ok<br/> Let's go<br/> Fo sho</p> </ul> into this: <ul> <li>Hi</li> <li>Ok</li> <li>Let's go</li> <li>Fo sho </li> </ul> ? – birgire Feb 15 '13 at 17:05

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.