0

In a plugin I have made (Snup Widget) I have made a metabox. In the sample here I have made two line with text. When I want to show the text in output in the widget it shows in one line, without break.

If I write the text in two lines, and save the post. The text is still in two lines when I load / open the post editor again. The problem is that they show up in one line in the output, not in two line as in editor.

enter image description here

I have now edited some of the code, but the problem is still that the text show up in one long line in the output.

enter image description here

The file is here: https://github.com/bjovaar/SNUP/tree/main/new-output

/*
 * ---------------------------------- *
 * Show the info in SNUP
 * ---------------------------------- *
 */
function snupwidget_upcoming_posts() { 

    $output = '';

    // The query to fetch future posts
    $the_query = new WP_Query(array( 
        'post_status' => 'future',
        'posts_per_page' => 1,
        'orderby' => 'date',
        'order' => 'ASC'
    ));
 

// The loop to display posts
if ( ! $the_query->posts ) {
  return sprintf(
    '<div class="snup_noplan">%s</div>',
    esc_html__('No planned posts yet.', 'snup-lang')
  );
}

$items = array();
foreach ($the_query->posts as $post) {

  $snuptext = get_post_meta( $post->ID, 'snuptext', true );
  if ( $snuptext && is_string( $snuptext ) ) {
    // turn linebreaks to <br>
    // @source https://www.php.net/manual/en/function.nl2br.php#73440
    $snuptext = str_replace(array("\r\n", "\r", "\n"), "<br />", $snuptext);
  } else {
    $snuptext = '';
  }
  
  $items[] = sprintf(
    '<li>
      %s
      <div class="snup_title">%s</div>
      <div class="snup_text">%s</div>
      <div class="snup_published">%s</div>
      <div class="snup_time">%s</div>
    </li>',
    get_the_post_thumbnail($post),
    esc_html( $post->post_title ),
    esc_html( $snuptext ),
    esc_html__('Published', 'snup-lang'),
    get_the_time('d.m.Y H:i', $post)
  );
}

return sprintf('<ul>%s</ul>', implode('', $items));
}
 



// Add shortcode
add_shortcode('snup-widget', 'snupwidget_upcoming_posts'); 
// Enable shortcode execution inside text widgets
add_filter('widget_text', 'do_shortcode');


?>

1 Answer 1

0

I had a new look now at the sprintf() part of the example I shared in my answer to your previous question. The thing that might contribute to the rendering problem is the esc_html( $snuptext ) line as it is basically disabling the <br> tags that are added with the str_replace() earlier. (I'm so used to using esc_html that I'm almost blind to it.)

So the sprintf would look like this,

$items[] = sprintf(
    '<li>
      %s
      <div class="snup_title">%s</div>
      <div class="snup_text">%s</div>
      <div class="snup_published">%s</div>
      <div class="snup_time">%s</div>
    </li>',
    get_the_post_thumbnail($post),
    esc_html( $post->post_title ),
    $snuptext,
    esc_html__('Published', 'snup-lang'),
    get_the_time('d.m.Y H:i', $post)
  );

I tested this locally in my sandbox WP and it seemed to work.

If you don't want to allow other html in the widget excerpt text, then you could do something like this when doing the str_replace.

$snuptext = str_replace(array("\r\n", "\r", "\n"), "<br />", strip_tags($snuptext));

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.