【问题标题】:Intercept an image before it's uploaded to Media Gallery在图像上传到媒体库之前截取图像
【发布时间】:2016-01-05 05:37:47
【问题描述】:

我有一个 WordPress 网站,我需要在其中发布一些带有 gif 缩略图的帖子。在研究了这个主题之后,我意识到我必须在它传递到媒体库之前拦截 gif。我什至不知道从哪里开始,因为我还没有找到任何钩子。

如何将正在上传的 gif 文件传递​​给自定义函数?有没有办法在 WordPress 中做到这一点?

【问题讨论】:

  • 你在 wordpress.stackexchange.com 上可能会有更好的运气

标签: php wordpress gif


【解决方案1】:

把这个函数放在你主题的你的functions.php中。

function custom_upload_filter( $file )
{
    // this line catch the name of the current upload file
    // and check the filetype
    $filetype = wp_check_filetype($file['name']);

    // then check the extension file if it isn't a gif file set the $file["error"] with an advertissement
    if( $filetype["ext"] != "gif" || $filetype["ext"] != "GIF )
        $file["error"] = "Please upload a file with a 'gif' extension !";

    return $file;
}
add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );

ps:在你的主题中添加这个功能后,你不能再上传除'gif'文件以外的其他文件

希望对你有所帮助。

【讨论】:

  • 谢谢,所以通过修改“only gifs”条件语句,我将能够对 gif 文件执行一项操作,对其他文件执行默认操作,对吗?
  • 上传后的 gif 文件你想做什么?
  • 将其分解为帧,将每一帧裁剪为特定大小,然后将裁剪后的帧再次合并为 gif。之后,我必须让 WordPress 将新的 gif 识别为媒体库项目。这将允许我上传 gif 并将它们设置为缩略图,而不会出现问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-03
  • 1970-01-01
  • 2015-04-29
  • 1970-01-01
  • 2015-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多