John Davidson

php - Wordpress how to use line breaks while escaping html tags

0 comments
Message:


I would like to separate the Welcome to %s and Thanks for creating account on %1$s by making each appear in a separate row.


They are currently jammed together/messed up when translating the phrases on a RTL site.


protected function send_account_email( $user_data, $user_id ) {

$to = $user_data['user_email'];
$subject = sprintf( esc_html__( 'Welcome to %s', 'my-plugin' ), get_option( 'blogname' ) );
$body = sprintf( esc_html__( 'Thanks for creating account on %1$s. Your username is: %2$s ',
'my-plugin' ), get_option( 'blogname' ), $user_data['user_login'] );
}


My Answer:
To add line breaks while escaping HTML tags in WordPress, you can use the `wpautop` function to automatically add paragraph tags to your text. Here's how you can modify your code to include line breaks:

php
protected function send_account_email( $user_data, $user_id ) {
$to = $user_data['user_email'];
$subject = sprintf( esc_html__( 'Welcome to %s', 'my-plugin' ), get_option( 'blogname' ) );
$body = sprintf( esc_html__( 'Thanks for creating account on %1$s.
Your username is: %2$s', 'my-plugin' ), get_option( 'blogname' ), $user_data['user_login'] );

// Add line breaks using wpautop
$body = wpautop( $body );

// Send email code here
}


By adding `
` tags in your string and then using `wpautop`, WordPress will automatically convert those `
` tags into line breaks when displaying the email content. This should help separate the phrases and make them appear in separate rows.

Rate this post

3 of 5 based on 3607 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us