最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - How to add HTML into error message

programmeradmin2浏览0评论

php I have a filter for limit max upload file size with custtom message. I want to add link there, but html does not work here. Can you help me someone?

Here is my code

add_filter("wp_handle_upload_prefilter", function ($file) {
  $file_size_limit = 1024; 
  $current_size = $file["size"];
  $current_size = $current_size / 1024;

  if ($current_size > $file_size_limit) {

    $file["error"] = sprintf(
      __("File is too large contact <a href=\"mailto:[email protected]\">admin</a>."));
  }
  return $file;
});


My problem is that I got a plain text instead of html.

Thanks for help.

php I have a filter for limit max upload file size with custtom message. I want to add link there, but html does not work here. Can you help me someone?

Here is my code

add_filter("wp_handle_upload_prefilter", function ($file) {
  $file_size_limit = 1024; 
  $current_size = $file["size"];
  $current_size = $current_size / 1024;

  if ($current_size > $file_size_limit) {

    $file["error"] = sprintf(
      __("File is too large contact <a href=\"mailto:[email protected]\">admin</a>."));
  }
  return $file;
});


My problem is that I got a plain text instead of html.

Thanks for help.

Share Improve this question asked Nov 11, 2020 at 14:42 SilmarionSilmarion 11 bronze badge 4
  • Those error messages sometimes include strings that come from insecure sources, so they're escaped for security reasons. You may not be able to enable HTML there without opening major security holes and exploits. As far as I can see, this happens in javascript on display – Tom J Nowell Commented Nov 11, 2020 at 14:59
  • Is there any way how to create my own notice only in this "if ($current_size > $file_size_limit)" case? I tried everything, echo. print etc but that does not works..:( I am able to create custom notice only when I use add_action('admin_notices', function () {…} But that show message at load page before upload media. – Silmarion Commented Nov 11, 2020 at 15:12
  • Not that I am aware of, what I've seen so far tells me this is a difficult question to figure out. Until then, you should use contact admin at [email protected] as a stopgap until a solution is found. The key difference with admin_notices is that PHP is what's outputting that markup, whereas on upload it's being returned in JSON for javascript to handle, because uploads happen via AJAX. You may get what you want if you use the simple uploader in the media section of the admin area, rather than the JS uploader in post edit screens etc – Tom J Nowell Commented Nov 11, 2020 at 15:13
  • @Silmarion did you ever work out how to add a link? Have the same issue. – SolaceBeforeDawn Commented May 23, 2023 at 0:58
Add a comment  | 

1 Answer 1

Reset to default 0

Please try this to add HTML:

$link = '<a href="mailto:[email protected]">admin</a>';
sprintf( __( 'File is too large contact %s.', 'my-textdomain' ), $link );

In this case the placeholder "%s" is replaced with the value of the variable "$link". And that variable is filled before.

发布评论

评论列表(0)

  1. 暂无评论