wordpress上传的文件在哪?
WordPress默认的上传文件的目录是/wp-content/uploads,而且文件是以年月的形式组织的,虽然我们可以去掉按照年月组织的选项,或者更改存储路径,但这个设置会应用到全局,不能按照特定条件选择特定目录存储文件。
WordPress上传默认设置
有时候将不同类型的文件分门别类存储,似乎比年月目录更有意义。例如幻灯片应该存储在slides目录下,下载文件应该存储在downloads文件夹下。就说幻灯片slideshow,我比较喜欢用自定义文章类型(Custom Post Type)实现,有些幻灯片脚本比较个性,不支持绝对路径,必须用相对路径,然后用base参数设置相对于哪个文件夹,这样幻灯片必须存储在某个特定的文件夹中,年月形式显然不满足要求。所以,我们需要条件化的设置上传目录。
为Custom Post Type设置上传目录
假设我要将所有在幻灯片类型的文章中上传的文件存储到/wp-content/uploads/slides文件夹中,将下面的代码放到主题的functions.php中即可
function custom_upload_directory( $uploads ) { $id = $_REQUEST['post_id']; $parent = get_post( $id )->post_parent; if( "post-type" == get_post_type( $id ) || "post-type" == get_post_type( $parent ) ) { $subdir = 'slides'; $uploads['subdir'] = $subdir; $uploads['path'] = $uploads['basedir'].DIRECTORY_SEPARATOR.$subdir; $uploads['url'] = $uploads['baseurl'].'/'.$subdir; } return $uploads; } add_filter( 'upload_dir', 'custom_upload_directory' );
将post-type替换成自己的自定义文章类型名称,将你要创建的子目录赋值给$subdir。
将文件保存到插件目录
下面的代码要用在插件中,文件会保存到插件目录下的uploads文件夹下。
/** * Change Upload Directory for Custom Post-Type * * This will change the upload directory for a custom post-type. Attachments will * now be uploaded to an "uploads" directory within the folder of your plugin. Make * sure you swap out "post-type" in the if-statement with the appropriate value... */ function custom_upload_directory( $args ) { $id = $_REQUEST['post_id']; $parent = get_post( $id )->post_parent; // Check the post-type of the current post if( "post-type" == get_post_type( $id ) || "post-type" == get_post_type( $parent ) ) { $args['path'] = plugin_dir_path(__FILE__) . "uploads"; $args['url'] = plugin_dir_url(__FILE__) . "uploads"; $args['basedir'] = plugin_dir_path(__FILE__) . "uploads"; $args['baseurl'] = plugin_dir_url(__FILE__) . "uploads"; } return $args; } add_filter( 'upload_dir', 'custom_upload_directory' );
如果要以年月形式保存,修改一下代码即可
$args['path'] = plugin_dir_path(__FILE__) . "uploads" . $args['subdir']; $args['url'] = plugin_dir_url(__FILE__) . "uploads" . $args['subdir'];
这段代码来自 http://wordpress.stackexchange.com/questions/35657/how-to-add-more-upload-directories/
为后台管理页面设定upload_dir
用wp_editor在后台管理页面(比如用add_menu_page创建的页面)创建一个媒体上传功能,希望所有从该页面上传的文件都保存到wp-content/uploads/myfolder目录下。
由 于ajax上传是直接调用wp-admin/async_upload.php文件,只能通过post_id获取post信息,而后台管理页面并非 post,所以判断什么时候应该更改upload_dir有些麻烦。此时,可以用采用判断页面referer的方法,用wp_get_referer() 函数获取引荐url,如果正好与我们的option page url想等,就更该目录。
function custom_upload_directory( $uploads ) { if( wp_get_referer() == 'http://domain.com/wp-admin/admin.php?page=myoptionpage'){ $subdir = 'myfolder'; $uploads['subdir'] = $subdir; $uploads['path'] = $uploads['basedir'].DIRECTORY_SEPARATOR.$subdir; $uploads['url'] = $uploads['baseurl'].'/'.$subdir; } return $uploads; } add_filter( 'upload_dir', 'custom_upload_directory' );
参考信息
filter:upload_dir是在wp_upload_dir()函数中调用的
$upload_dir = wp_upload_dir(); $upload_dir now contains something like the following (if successful) Array ( [path] => C:\path\to\wordpress\wp-content\uploads\2010\05 [url] => http://example.com/wp-content/uploads/2010/05 [subdir] => /2010/05 [basedir] => C:\path\to\wordpress\wp-content\uploads [baseurl] => http://example.com/wp-content/uploads [error] => )
更多WordPress技术文章,请访问WordPress教程栏目!
The above is the detailed content of Where are the files uploaded by wordpress?. For more information, please follow other related articles on the PHP Chinese website!

Enable comments on your WordPress website to provide visitors with a platform to participate in discussions and share feedback. To do this, follow these steps: Enable Comments: In the dashboard, navigate to Settings > Discussions, and select the Allow Comments check box. Create a comment form: In the editor, click Add Block and search for the Comments block to add it to the content. Custom Comment Form: Customize comment blocks by setting titles, labels, placeholders, and button text. Save changes: Click Update to save the comment box and add it to the page or article.

How to copy WordPress subsites? Steps: Create a sub-site in the main site. Cloning the sub-site in the main site. Import the clone into the target location. Update the domain name (optional). Separate plugins and themes.

The steps to create a custom header in WordPress are as follows: Edit the theme file "header.php". Add your website name and description. Create a navigation menu. Add a search bar. Save changes and view your custom header.

Enable comments in WordPress website: 1. Log in to the admin panel, go to "Settings" - "Discussions", and check "Allow comments"; 2. Select a location to display comments; 3. Customize comments; 4. Manage comments, approve, reject or delete; 5. Use <?php comments_template(); ?> tags to display comments; 6. Enable nested comments; 7. Adjust comment shape; 8. Use plugins and verification codes to prevent spam comments; 9. Encourage users to use Gravatar avatar; 10. Create comments to refer to

You can install the FTP plug-in through WordPress, configure the FTP connection, and then upload the source code using the file manager. The steps include: installing the FTP plug-in, configuring the connection, browsing the upload location, uploading files, and checking that the upload is successful.

How to copy WordPress code? Copy from the admin interface: Log in to the WordPress website, navigate to the destination, select the code and press Ctrl C (Windows)/Command C (Mac) to copy the code. Copy from a file: Connect to the server using SSH or FTP, navigate to the theme or plug-in file, select the code and press Ctrl C (Windows)/Command C (Mac) to copy the code.

WordPress Error Resolution Guide: 500 Internal Server Error: Disable the plug-in or check the server error log. 404 Page not found: Check permalink and make sure the page link is correct. White Screen of Death: Increase the server PHP memory limit. Database connection error: Check the database server status and WordPress configuration. Other tips: enable debug mode, check error logs, and seek support. Prevent errors: regularly update WordPress, install only necessary plugins, regularly back up your website, and optimize website performance.

How to turn off a comment in WordPress? Specific article or page: Uncheck Allow comments under Discussion in the editor. Whole website: Uncheck "Allow comments" in "Settings" -> "Discussion". Using plug-ins: Install plug-ins such as Disable Comments to disable comments. Edit the topic file: Remove the comment form by editing the comments.php file. Custom code: Use the add_filter() function to disable comments.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor