douqun1977 2017-11-29 03:54
浏览 75

esc_attr()和sanitize_text_field()之间的确切区别

If any WordPress dev can help me to understand what is the exact difference between functions esc_attr() and sanitize_text_field() ?

I would like to submit email value to the database first and then retrieve the same in different classes that I've created. With sanitize_text_field I am able to remove html tags like h2, h3 and other but I do not quite understand what exactly est_attr function does while echoing both. Below is the snippet of my function, any detailed explanation with example will help me and others who wants to understand it thoroughly. Or are there any better alternative php or wp functions to use while submitting such information to database and retrieve the same?

public static function smtp_email_id()
{
    $smtp_email_id = esc_attr( get_option( 'smtp_email_id' ) );
    echo '<input type="email" class="regular-text" name="smtp_email_id" value="'.$smtp_email_id.'" placeholder="username@email.com" />';
}

Thank you guys

  • 写回答

1条回答 默认 最新

  • duanci1858 2017-11-29 05:09
    关注
    1. esc_attr($str) - Escaping for HTML attributes. Encodes the <, >, &, ” and ‘ (less than, greater than, ampersand, double quote and single quote) characters.

    2. sanitize_text_field($str) - Behinds the scenes, the function does the following:

      1. Checks for invalid UTF-8 (uses wp_check_invalid_utf8()) Converts single < characters to entity
      2. Strips all tags
      3. Remove line breaks, tabs and extra white space
      4. Strip octets
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用