Wordpress 如何在转义 html 标签时使用换行符

问题描述 投票:0回答:2

我想通过使每个出现在单独的行中来分隔

Welcome to %s
Thanks for creating account on %1$s

RTL
网站上翻译短语时,它们目前被挤在一起/搞乱了。

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'] );
}
php html wordpress line-breaks email-confirmation
2个回答
3
投票

您需要一个“换行符”来将它们分开!您可以使用

html
标签,例如:

  • br
    标签
  • p
    标签
  • h1
    标签

仅举几例!

但是您正在使用

esc_html__
来翻译并转义
html
。为什么需要使用
esc_html__
从数据库中检索博客名称?为什么?

话虽如此,您可以同时使用一种白名单技术来

translate
escape unwanted html

使用

wp_kses
您可以为允许的
html
标签定义“白名单”并转义其余标签。

您可以阅读更多相关内容:

wp_kses
文档

这篇关于白名单 html 标签的文章


所以你的代码将是这样的:

使用

<br>
标签:

protected function send_account_email( $user_data, $user_id ) {

  $whitelist_tags = array(

    'br' => array(),
  
  );
        
  $to      = $user_data['user_email'];

  $subject = sprintf(wp_kses(__('Welcome to %s <br>', 'my-plugin'), $whitelist_tags), 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'] );
}

或使用

<p>
标签:

protected function send_account_email( $user_data, $user_id ) {

  $whitelist_tags = array(

    'p' => array()
  
  );
        
  $to      = $user_data['user_email'];
  
  $subject = sprintf(wp_kses(__('<p>Welcome to %s </p>', 'my-plugin'), $whitelist_tags), 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'] );
}

或使用

<h1>
标签:

protected function send_account_email( $user_data, $user_id ) {

  $whitelist_tags = array(

    'h1' => array(),
  
  );
        
  $to      = $user_data['user_email'];

  $subject = sprintf(wp_kses(__('<h1>Welcome to %s </h1>', 'my-plugin'), $whitelist_tags), 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'] );
}

注:

  • $whitelist_tags
    是一个数组,因此您可以向其中添加多个标签!
  • 另外,我只在您的
    $subject
    变量中使用了这些标签,如果需要,您也可以在
    $body
    变量中使用确切的技术!
  • 我还使用了
    __()
    wp_kses
    的组合来代替
    esc_html__
    ,以便
    translate
    escape unwanted html

-1
投票

感谢您的信息,希望我的网站sportsbet也能取得成功

© www.soinside.com 2019 - 2024. All rights reserved.