详解WordPress开发中过滤属性以及Sql语句的函数使用
<p><strong>esc_attr()(过滤属性)</strong><br>
一般在写 Html 代码的标签属性的时候会是下边的格式:</p>
<div>
<div>
<div id="highlighter_59220">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code><</code><code>input</code> <code>type</code><code>=</code><code>"text"</code> <code>name</code><code>=</code><code>"rep"</code> <code>value</code><code>=</code><code>"rep_value"</code> <code>/></code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
那如果 value 属性是动态输出的呢?</p>
<div>
<div>
<div id="highlighter_769897">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code><input type=</code><code>"text"</code> <code>name=</code><code>"rep"</code> <code>value=</code><code>"<?php echo get_option( 'rep_value' ); ?>"</code> <code>/></code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
但是,如果动态输出的属性里有双引号、尖括号等特殊字符,Html 代码就会被打乱,这时就可以使用 esc_attr() 函数对输出的属性进行转义。</p>
<p>
使用方法</p>
<div>
<div>
<div id="highlighter_585439">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>esc_attr( </code><code>$text</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
参数</p>
<p>
$text (字符串)(必须)要转义的字符串。 默认值:None</p>
<p>
返回值</p>
<p>
返回转义后的字符串。</p>
<p>
例子</p>
<div>
<div>
<div id="highlighter_567472">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code><input type=</code><code>"text"</code> <code>name=</code><code>"rep"</code> <code>value=</code><code>"<?php echo esc_attr( get_option( 'rep_value' ) ); ?>"</code> <code>/></code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
其它</p>
<p>
此函数位于:wp-includes/formatting.php</p>
<p>
</p>
<p>
<strong>esc_sql()(过滤 Sql 语句)</strong><br>
esc_sql() 用来过滤准备添加到 Sql 语句里边的字符串,防止 Sql 注入和 Sql 语句被数据干扰出现异常。</p>
<p>
用法</p>
<div>
<div>
<div id="highlighter_626004">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>esc_sql( </code><code>$data</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
参数</p>
<p>
$data</p>
<p>
(字符串)(必须)要过滤的字符串。</p>
<p>
默认值:None</p>
<p>
返回值</p>
<p>
(字符串)返回过滤后的字符串,可以直接添加到 Sql 语句里。</p>
<p>
例子</p>
<div>
<div>
<div id="highlighter_612648">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
</td>
<td>
<div>
<div>
<code>$name</code> <code>= esc_sql( </code><code>$name</code> <code>);</code>
</div>
<div>
<code>$status</code> <code>= esc_sql( </code><code>$status</code> <code>);</code>
</div>
<div>
<code>$wpdb</code><code>->get_var( </code><code>"SELECT something FROM table WHERE foo = '$name' and status = '$status'"</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
更多</p>
<p>
此函数位于:wp-includes/formatting.php</p>
頁:
[1]