I am looking for a solution to add markup to a custom field with multiple lines. Much like wpautop, but with customization. I'm not quite sure to approach this with a custom function or something built into WordPress.

Custom Field text would be:

Line 1
Line 2
Line 3

Code output would add markup:

<span>Line 1</span>
<span>Line 2</span>
<span>Line 3</span>

1 Answer 1


I believe this is what you are after:

// define $post_id, $key, $single
$multipleLineValue = get_post_meta($post_id, $key, $single);

// Convert into an array where desired code can be added to the output
$multipleLineValue = explode("\n",$multipleLineValue);
$output = "";
foreach ($multipleLineValue as $lineValue) {
   $output .= "<span>".$lineValue."</span><br>";
echo $output;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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