dongyong8071 2019-08-09 05:10
浏览 99

如何在PHP中显示自定义错误消息?

I am trying to limit file upload size on my site and using the following code (which works fine):

function limit_upload_size( $file ) {

// Set the desired file size limit
$file_size_limit = 1024; // 1MB in KB

// exclude admins
if ( ! current_user_can( 'manage_options' ) ) {

    $current_size = $file['size'];
    $current_size = $current_size / 1024; //get size in KB

    if ( $current_size > $file_size_limit ) {
        $file['error'] = sprintf( __( 'ERROR: File size limit is %d KB.' ), $file_size_limit );
    }

}

return $file;

}
add_filter ( 'wp_handle_upload_prefilter', 'limit_upload_size', 10, 1 );

The only problem I've encountered is that the error message doesn't display. When I change

$file['error'] = sprintf( __( 'ERROR: File size limit is %d KB.' ), $file_size_limit );

to

 $file['error'] = sprintf( __e( 'ERROR: File size limit is %d KB.' ), $file_size_limit );

I see the following error message instead: Error #-200: HTTP Error instead of the custom error message.

What is wrong with this code?

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dqwh1203 2019-08-09 05:26
    关注

    That's because __() returns the translated text, while _e() displays/echos it. And as you are inside sprintf() function, you need the returned translated text, not display it to the screen.

    _e() codex reference, __() codex reference

    Thanks.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?