【问题标题】:Wordpress how to use line breaks while escaping html tagsWordpress如何在转义html标签时使用换行符
【发布时间】:2021-09-27 00:24:24
【问题描述】:

我想将Welcome to %sThanks 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


    【解决方案1】:

    您需要一个“换行符”将它们分开!您可以使用html 标签,例如:

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

    仅举几例!

    但是您正在使用 esc_html__ 翻译和转义 html。为什么需要使用esc_html__ 从数据库中检索博客名称?为什么?

    话虽如此,您可以同时对translateescape unwanted html 使用一种白名单技术。

    使用wp_kses,您将能够为允许的html 标签定义一个“白名单”并转义其余标签。

    您可以阅读更多相关信息:

    wp_ksesDocs

    This post on whitelisting html tags


    所以你的代码应该是这样的:

    使用<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'] );
    }
    

    或者使用&lt;p&gt;标签:

    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'] );
    }
    

    或者使用&lt;h1&gt;标签:

    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__,以便translateescape unwanted html

    【讨论】:

      猜你喜欢
      • 2017-05-07
      • 2012-10-22
      • 2022-11-18
      • 2013-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多