在WordPress中只允许上传一个格式

huangapple go评论115阅读模式
英文:

Allow uploading only one format in WordPress

问题

如何阻止上传所有格式,只允许上传一种格式?

使用以下代码,您可以阻止上传所需的格式,但是使用此代码可能会忘记阻止某种格式。我想创建一个条件,如果文件是jpg格式,则允许上传,不允许上传除jpg之外的任何格式。这样,我就不再需要搜索每种格式并阻止它们。

  1. function cc_mime_types( $mimes ) {
  2. unset( $mimes['png'] );
  3. return $mimes;
  4. }
  5. add_filter('upload_mimes', 'cc_mime_types');
英文:

How to block upload of all formats, allow only one format to upload?

With the following code, you can block the upload of the desired formats, But with this code I might forget to block a format, I want to make a condittion that if the file was in jpg format, it would upload and not allow uploading any format other than jpg. In this way, I no longer need to search for each format and block them.

  1. function cc_mime_types( $mimes ) {
  2. unset( $mimes['png'] );
  3. return $mimes;
  4. }
  5. add_filter('upload_mimes', 'cc_mime_types');

答案1

得分: 1

以下是已翻译好的代码部分:

  1. With the help of my good friend **CBroe**, my problem was solved with the following code :
  2. function cc_mime_types( $mimes ) {
  3. $mimes = array(
  4. 'jpg|jpeg' => 'image/jpeg',
  5. );
  6. return $mimes;
  7. }
  8. add_filter('upload_mimes', 'cc_mime_types');
  9. CBroe, Thank you very much.

请注意,我已保留了代码的原始格式和标记。

英文:

With the help of my good friend CBroe, my problem was solved with the following code :

  1. function cc_mime_types( $mimes ) {
  2. $mimes = array(
  3. 'jpg|jpeg' => 'image/jpeg',
  4. );
  5. return $mimes;
  6. }
  7. add_filter('upload_mimes', 'cc_mime_types');

CBroe, Thank you very much.

huangapple
  • 本文由 发表于 2023年4月17日 20:04:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034976.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定