Skip to content

修改wordpress xmlrpc API搞定通过vimrepress上传图片时没办法显示缩略图的问题

2011 五月 30
by 荒野无灯

发现通过vimrepress 的BlogUpload 功能上传图片时,获取的图片虽然是带链接的,但是img标签的src指向的是原始大小的图片,如果图片本身很大的话,显示在博客上就不太好看了。要是能像在wp后台插入图片一样生成带缩略图指向原始图片的链接就好了。
查看了下wp的xmlrpc api 实现,发现上传图片其实是通过 mw_newMediaObject方法上传的。该方法最后返回的数据类型和数量,基本上不可能hook,再说了,wp_handle_upload 这个filter也不只是在xmlrpc server这里用到了。全局hook不太好,还是修改单个文件比较实际。
mw_newMediaObject只返回三个参数:file , url ,type
分别是文件名,文件URL,文件的MIME类型。
因此我这里给它加一个返回值:thumb_url .

wp-includes/class-wp-xmlrpc-server.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    //把原文件最后一行注释掉
    //return apply_filters( 'wp_handle_upload', array( 'file' => $name, 'url' => $upload[ 'url' ], 'type' => $type ), 'upload' );
    //get thumbnail via xmlrpc
    //by 荒野无灯 2011/05/30
    $ret=apply_filters( 'wp_handle_upload', array( 'file' => $name, 'url' => $upload[ 'url' ], 'type' => $type ), 'upload' );
    if('image' == substr($type,0,5) )
    {
        $thumb = wp_get_attachment_image_src( $id , 'medium' );
        $thumb_url = array('thumb_url'=> $thumb[0] );
        return $ret+$thumb_url;
    }
    else
    {
    return $ret;
    }

然后修改vimrepress插件中的image_template,把img标签的里的%(url)s 参数修改 为 %(thumb_url)s 即可。

本文由荒野无灯使用vimrepress发布。

喜欢这篇文章吗?

请订阅本站 RSS feed填写您的邮件地址,订阅我们的精彩内容:,欢迎点击这里捐赠以支持荒野无灯转播到腾讯微博 转播到腾讯微博

作者:荒野无灯
出处:Hacklog【Hacklog】

声明: 本站遵循 署名-非商业性使用-相同方式共享 3.0 共享协议. 转载请注明转自Hacklog【荒野无灯weblog】

本文链接: http://ihacklog.com/?p=4525

3 Responses Post a comment
  1. 一月 9, 2012

    请问 我用这个修改了 得到的thumb_url是空。 看 :twisted: wp-upload目录下也有生成缩略图

    是不是漏掉了什么呢 本人菜鸟 呵呵

    我的邮箱 share2 at foxmail.com

Trackbacks & Pingbacks

  1. Hacklog » 修正vimrepress的一个bug

Leave a Reply

Allowed Tags - You may use these HTML tags and attributes in your comment.

<a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <q cite=""> <strong>

 :wink:  :-|  :-x  :twisted:  :)  8-O  :(  :roll:  :-P  :oops:  :-o  :mrgreen:  :lol:  :idea:  :-D  :evil:  :cry:  8)  :arrow:  :-?  :?:  :!:

Note: You may use basic HTML in your comments. Your email address will not be published.

Subscribe to this comment feed via RSS