1

I tried possible solutions to save \n as <br /> from my custom post type <textarea> metabox but it is not working.

I am using a custom made theme and there is no additional plugin installed.

function abhijaan_itinerary_metabox( $post ) {
    wp_nonce_field( 'trek_itinerary', 'trek_itinerary_nonce' );
    $content = get_post_meta( $post->ID, '_trek_itinerary', true );
    $content = preg_replace( '#<br\s*/?>#i', "\n", $content );
    ?>
    <textarea class="trek_inputs required" name="txtItinerary" id="txtItinerary" cols="30" rows="10" placeholder="Itinerary" required><?php echo $content ?></textarea>
    <?php
}


function save_custom_metaboxes( $post_id ) {
   ...
   $itinerary = sanitize_text_field( $_POST['txtItinerary'] );
   $itinerary = nl2br( $itinerary ); // NOT WORKING!
   update_post_meta( $post_id,  '_trek_itinerary', $itinerary );
   ...
}

add_action( 'save_post',  'save_custom_metaboxes' );

Before usinng <textarea>, I tried WYSIWYG editor as well. That too had the same issue.

function abhijaan_itinerary_metabox( $post ) {
    wp_nonce_field( 'trek_itinerary', 'trek_itinerary_nonce' );
    $content = get_post_meta( $post->ID, '_trek_itinerary', true );

    wp_editor(
            $content,
            'txtItinerary',
            array( 'media_buttons' => false )
    );
}

I am expecting the data to be saved with <br /> in wp_postmeta table, but it is not happening. I have multiple metaboxes which are using <textarea>. What is going wrong? :(

UPDATE 1

In fact default post editor (content area) is not saving paragraphs! But if I use other HTML formatting like bullets, they are saved. It is only the <p> tag which is not saving at all from anywhere!

UPDATE 2
I am using WordPress 5.2.2. Classic Editor plugin is not installed. However, in my CPT, the default editor is the old one however block editor is coming up when I am writing a normal Post (not CPT). I am fine with the old editor in my CPT, but don't know why the paragraphs are always ignored!

UPDATE 3 (Screenshots)

Backend: enter image description here

Frontend: enter image description here

UPDATE 4 wpautop( the_content() ) has resolved the content paragraph issue on frontend.

1 Answer 1

3

From the reference/description of sanitize_text_field():

  • Checks for invalid UTF-8,

  • Converts single < characters to entities

  • Strips all tags

  • Removes line breaks, tabs, and extra whitespace

  • Strips octets

So a simple fix is, use sanitize_textarea_field():

The function is like sanitize_text_field(), but preserves new lines (\n) and other whitespace, which are legitimate input in textarea elements.

// In save_custom_metaboxes()
$itinerary = sanitize_textarea_field( $_POST['txtItinerary'] ); // use this one
//$itinerary = sanitize_text_field( $_POST['txtItinerary'] );   // and not this

Additional Notes

Also, you should use esc_textarea() (although the output may not contain HTML tags):

<textarea class="trek_inputs required" name="txtItinerary"...><?php echo // wrapped for clarity
  esc_textarea( $content ); ?></textarea>

And you'd also want to remove line break after each <br />:

$itinerary = sanitize_textarea_field( $_POST['txtItinerary'] );
$itinerary = nl2br( $itinerary );
// Removes line break after each <br />, if any.
$itinerary = preg_replace( "#<br />(\r\n|\n|\r)#", '<br />', $itinerary );
2
  • Thank you! This works great for metaboxes. But why the paragraphs of the main content of my post are not rendering? It is not a meta! Please suggest. I have updated my original question with screenshots. Commented Jul 30, 2019 at 4:56
  • 1
    Well, you got it sorted already - using wpautop(). And actually, you could also use that function with the meta boxes - wpautop( $itinerary ) instead of nl2br( $itinerary ). That way, you wouldn't need the $content = preg_replace( '#<br\s*/?>#i', "\n", $content ); in abhijaan_itinerary_metabox().
    – Sally CJ
    Commented Jul 30, 2019 at 5:21

Your Answer

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

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