强化 WordPress 2.0.1 文件上传功能
WordPress 2.0.1 相对于 2.0 的一个重要修正就是“可以指定文件上传路径”。但这一功能相对 WordPress 1.5.x 版本还不够完善。在 WP 1.5 中,我们可以设定上传文件存放的绝对路径,同时可以设定访问上传文件的 URL 前缀。通过这个功能,我们可以将文件上传到其它路径下,从而借助服务器设置轻松实现一些功能(例如防盗链);而对于访问量巨大的网站,像图片、附件这样的静态内容可以通过单独使用一个静态编译的 apache,减小服务器负担,提高吞吐量。
在 WP 2.0.1 中,这个功能被弱化了。只能设定文件存放的相对路径,而且无法设定 URL 前缀。对于从 1.5.x 版一直使用这个功能的网站(例如本站)来说,移植是十分麻烦的。于是我通过简单修改 WordPress,为 WordPress 2.0.1 加上了这个功能。(懒得自己改的朋友可以在这里下载改好的文件)
在 wp-includes/functions-post.php 文件中,搜索 function wp_upload_dir(),按照如下修改这个函数:
- <?php
- // Returns an array containing the current upload directory's path and url, or an error message.
- function wp_upload_dir() {
- $siteurl = get_settings('siteurl');
- //prepend ABSPATH to $dir and $siteurl to $url if they're not already there
- $path = str_replace(ABSPATH, '', trim(get_settings('upload_path')));
- if (substr($path,0,1) == '/') {
- // an absolute path
- $dir = $path;
- $url = trailingslashit(get_settings('fileupload_url'));
- } else {
- $dir = ABSPATH . $path;
- $url = trailingslashit($siteurl) . $path;
- }
- if ( $dir == ABSPATH ) { //the option was empty
- $dir = ABSPATH . 'wp-content/uploads';
- }
- if ( defined('UPLOADS') ) {
- $dir = ABSPATH . UPLOADS;
- $url = trailingslashit($siteurl) . UPLOADS;
- }
- if ( get_settings('uploads_use_yearmonth_folders')) {
- // Generate the yearly and monthly dirs
- $time = current_time( 'mysql' );
- $y = substr( $time, 0, 4 );
- $m = substr( $time, 5, 2 );
- $dir = $dir . "/$y/$m";
- $url = $url . "$y/$m";
- }
- // Make sure we have an uploads dir
- if ( ! wp_mkdir_p( $dir ) ) {
- $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), $dir);
- return array('error' => $message);
- }
- $uploads = array('path' => $dir, 'url' => $url, 'error' => false);
- return apply_filters('upload_dir', $uploads);
- }
- ?>
再打开 wp-admin/options-misc.php,改成如下内容:
- <?php
- require_once('admin.php');
- $title = __('Miscellaneous Options');
- $parent_file = 'options-general.php';
- include('admin-header.php');
- ?>
- <div class="wrap">
- <h2><?php _e('Miscellaneous Options') ?></h2>
- <form method="post" action="options.php">
- <fieldset class="options">
- <legend><?php _e('Uploading'); ?></legend>
- <table class="editform optiontable">
- <tr valign="top">
- <th scope="row"><?php _e('Store uploads in this folder'); ?>:</th>
- <td><input name="upload_path" type="text" id="upload_path" class="code" value="<?php echo get_settings('upload_path'); ?>" size="40" />
- <br />
- <?php _e('Default is <code>wp-content/uploads</code>'); ?>
- </td>
- </tr>
- <tr valign="top">
- <th scope="row"><?php _e('Uploads URL'); ?>:</th>
- <td><input name="fileupload_url" type="text" id="fileupload_url" class="code" value="<?php echo get_settings('fileupload_url'); ?>" size="40" />
- </td>
- </tr>
- <tr>
- <td></td>
- <td>
- <label for="uploads_use_yearmonth_folders">
- <input name="uploads_use_yearmonth_folders" type="checkbox" id="uploads_use_yearmonth_folders" value="1" <?php checked('1', get_settings('uploads_use_yearmonth_folders')); ?> />
- <?php _e('Organize my uploads into month- and year-based folders'); ?>
- </label>
- </td>
- </tr>
- </table>
- </fieldset>
- <p><input name="use_linksupdate" type="checkbox" id="use_linksupdate" value="1" <?php checked('1', get_settings('use_linksupdate')); ?> />
- <label for="use_linksupdate"><?php _e('Track Links’ Update Times') ?></label></p>
- <p>
- <label><input type="checkbox" name="hack_file" value="1" <?php checked('1', get_settings('hack_file')); ?> /> <?php _e('Use legacy <code>my-hacks.php</code> file support') ?></label>
- </p>
- <p class="submit">
- <input type="hidden" name="action" value="update" />
- <input type="hidden" name="page_options" value="hack_file,use_linksupdate,uploads_use_yearmonth_folders,upload_path,fileupload_url" />
- <input type="submit" name="Submit" value="<?php _e('Update Options') ?> »" />
- </p>
- </form>
- </div>
- <?php include('./admin-footer.php'); ?>
进入管理后台,选项->杂项 即可进行设置。以上代码在本站测试正常并已经应用。
原文链接**:花儿开了 - 强化 WordPress 2.0.1 文件上传功能
** 本博客文章欢迎转载,但请务必保留原文链接!同时,本博文章不欢迎任何形式的派生及篡改,如需引用,请使用引用通告(Trackback) - http://blog.istef.info/2006/02/10/enhance-word.../trackback/。商业网站使用请务必先取得作者授权!





不错,我还在用1.5,不过还是用了插件来解决这个问题,请教一下,你post里面图片和文字的融合用什么插件作的?
不知道是不是feedburner的rss有问题,bloglines上没更新提示,这两次倒都是先看到了Blogging Pro China的引用文章
to netear:没有用任何插件,wp2.0的图片上传机制已经足够好用了。:mrgreen:
啊哈,也是dreamhost的用户啊……
我也才搬去不久,百废待兴哪…
模板很漂亮:)
你的图片格式是怎么修改css来实现的呢?
贵站用的哪款代码高亮插件?
to moyii: coolcode
to iStef:
如何将图片嵌入到文字中,而不是另起一行呢?
to netear: <img src=”xxx” alt=”xxx” align=”left” />
或者 <img src=”xxx” alt=”xxx” style=”float:left” />
我比较偏爱后一种写法,兼容性更好
文件不能下载 ?
hehe不错,很强啊,,blog做得也很不错。不过文件不能下