You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义WordPress用户注册页开发遇到PHP Warning报错如何解决

报错原因

PHP 7.2及以上版本收紧了count()函数的入参校验:如果传入的参数不是数组、也不是实现了Countable接口的对象,就会抛出该警告。旧版本PHP遇到这类非法入参时会静默返回0,不会触发告警,所以你之前在其他主机运行相同代码没有问题。

修复方案

直接替换原有代码即可:

<?php if ( isset( $attributes['errors'] ) && is_countable( $attributes['errors'] ) && count( $attributes['errors'] ) > 0 ) : ?>
    <?php foreach ( $attributes['errors'] as $error ) : ?>
        <p>
            <?php echo esc_html( $error ); ?>
        </p>
    <?php endforeach; ?>
<?php endif; ?>

补充说明

  • 新增的isset($attributes['errors'])判断是为了避免$attributes数组中不存在errors键时抛出PHP Notice
  • is_countable()是PHP 7.3原生支持的校验函数,刚好适配你当前的运行环境,用来判断变量是否可以安全传入count()
  • 额外给输出的错误信息加了esc_html()转义,符合WordPress官方安全编码规范,避免XSS注入风险,属于可选优化项,你也可以根据需求去掉。

内容的提问来源于stack exchange,提问作者Æthelstan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 16:15:00