最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - WordPress nl2br is not converting newline to html line break when saving metabox value

programmeradmin1浏览0评论

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:

Frontend:

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

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:

Frontend:

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

Share Improve this question edited Jul 30, 2019 at 5:08 Subrata Sarkar asked Jul 29, 2019 at 14:32 Subrata SarkarSubrata Sarkar 2395 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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 );
发布评论

评论列表(0)

  1. 暂无评论