我的一个插件有问题。
日志文件显示:
PHP警告:stripslashes()要求参数1为字符串,数组在第125行的/mnt/web008/c1/24/57250724/htdocs/WordPress_01/wp-content/plugins/cookie law-info/PHP/shortodes.PHP中给定
看起来有一个给定的aray,但需要一个字符串?我不知道怎样才能解决这个问题。
/** Returns HTML for a standard (green, medium sized) 'Accept' button */
function cookielawinfo_shortcode_accept_button( $atts ) {
extract( shortcode_atts( array(
'colour' => 'green'
), $atts ) );
// Fixing button translate text bug
// 18/05/2015 by RA
$defaults = array(
'button_1_text' => ''
);
$settings = wp_parse_args( cookielawinfo_get_admin_settings(), $defaults );
/*This is line 125:*/ return '<a href="#" id="cookie_action_close_header" class="medium cli-plugin-button">' . stripslashes( $settings ) . '</a>';
}
好吧,这个错误本身是不言自明的。
函数stripslashes
期望其参数为字符串。快速查看一下Wordpress文档,就会发现wp_parse_args
的返回值是一个数组,这意味着$settings
变量是一个阵列,而不是字符串,因此在stripslashes
中将其作为参数传递会导致错误。
您可以在数组上使用stripslashes
,但是它需要更多的工作。以下是PHP文档中给出的示例。
<?php
function stripslashes_deep($value) {
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
return $value;
}
// Example
$array = array("f\'oo", "b\'ar", array("fo\'o", "b\'ar"));
$array = stripslashes_deep($array);
// Output
print_r($array);
?>
https://developer.wordpress.org/reference/functions/wp_parse_args/http://php.net/manual/en/function.stripslashes.php
EDIT:可能值得注意的是,stripslashes_deep
将返回一个数组。如果这不是所需的输出,则通过内爆函数包装stripslashes_deep函数的调用,将其转换为字符串。
implode(stripslashes_deep($settings))