我现在对VIP编码标准有点问题。我正在打印一份管理通知,如下所示:
add_action( \'admin_notices\', function() {
$class = \'notice notice-warning is-dismissible\';
$message = __( \'We suggest that you use the <b>API Key file</b> for your API Keys.\', \'package-wordpress\' );
print wp_sprintf( \'<div class="%s"><p>%s</p></div>\', $class, $message );
});
它之所以成功,是因为它打印并显示了通知,但是,当我运行代码标准检查时,它会抛出转义函数错误。
----------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 1 LINE
----------------------------------------------------------------------
101 | ERROR | Expected next thing to be an escaping function (see
| | Codex for \'Data Validation\'), not \'$class\'
101 | ERROR | Expected next thing to be an escaping function (see
| | Codex for \'Data Validation\'), not \'$message\'
----------------------------------------------------------------------
我对这个问题做了一些研究,发现它确实希望我在将html打印到屏幕上时避开html,但是当我这样做时,它当然会删除在消息中包含html的功能。在通过编码标准的同时,有什么建议可以这样做?
最合适的回答,由SO网友:Bjorn 整理而成
处理这个问题的一种方法是在分配$message
变量,然后使用wp_kses
当您想要输出时。您还可以在消息前后输出div和段落标记,这样就不需要wp_sprintf
在这种情况下。
add_action( \'admin_notices\', function() {
$message = __( \'We suggest that you use the <b>API Key file</b> for your API Keys.\', \'package-wordpress\' );
echo \'<div class="notice notice-warning is-dismissible"><p>\' . wp_kses( $message, array( \'b\' => array() ) ) . \'</p></div>\';
});
如果您想让它更接近原始版本,可以执行以下操作:
add_action( \'admin_notices\', function() {
$class = \'notice notice-warning is-dismissible\';
$message = wp_sprintf( __( \'<div class="%s"><p>We suggest that you use the <b>API Key file</b> for your API Keys.</p></div>\', \'package-wordpress\' ), $class );
echo wp_kses( $message, array(
\'div\' => array( \'class\' => array() ),
\'p\' => array(),
\'b\' => array(),
));
});