0

No matter what I do (stripping html tags or not) the following statement displays the result on 2 lines rather than one:

$text = sprintf( '<p>%1$s %2$s</p>', wp_filter_nohtml_kses( the_author_meta('user_firstname') ), __( 'hasn\'t published any articles yet.', 'monochrome-pro' ) );

Any idea why this is happening?

  • There's nothing wrong with the code here, so without being able to see the actual output on a real page, it's impossible to say. Also, wp_filter_nohtml_kses() is probably overkill for this. kses functions can be slow and are better used for saving to the database. For output esc_html() should suffice. – Jacob Peattie Jan 31 '18 at 9:17
  • html outputs: Dana <div class="entry"><p>hasn't published any articles yet.</p></div> It keeps injecting a div for some reason. – Eric Trometer Jan 31 '18 at 9:27
  • It is a Genesis child theme if that makes any difference – Eric Trometer Jan 31 '18 at 9:29
1

Note that the_author_meta() doesn't return the meta value, but echoes it. That's why the name is suddenly outside the <p> element.

Use get_the_author_meta() if you need to return (and do something with) the field, rather than just display it.

I'd highly recommend you to change your use usage of __() too, because that is not useful for translators as it's missing the name as context.

/* translators: %s: user's first name */
$text = sprintf(
  __( "%s hasn't published any articles yet.", 'monochrome-pro' ),
  esc_html( get_the_author_meta( 'user_firstname' ) )
);

echo '<p>' . $text . '</p>';

Note: if you don't trust your translations, you should use esc_html() on $text instead of the user meta. As mentioned in the comments, using KSES functions is a bit overkill. If you want to get rid of any HTML tags (vs. just escaping them) I'd use wp_strip_all_tags().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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