
內容簡介
啟用此外掛可將您的網誌上所有找不到的上傳圖片導向您最喜愛的替代圖片服務,例如 placehold.it 或 placekitten.com。請注意,雖然小貓很可愛,但 placehold.it 服務更實用,因為所有圖片尺寸的背景和前景色彩都相同(例如在圖片輸出格式為全尺寸或縮圖的圖庫中)能輕鬆區別每張圖片。您可以通過定義 MISSING_UPLOADED_IMAGE_PLACEHOLDER_BUILTIN 為 placekitten_color 或 placekitten_grayscale,或者透過 missing_uploaded_image_placeholder_builtin 做為篩選器來將默認的 placehold.it 服務更換成 placekitten.com。
此外掛僅建議開發期使用。您應該在本地開發環境(例如 Vagrant 或 XAMPP)或編寫的清單伺服器上啟用此外掛。這個外掛在團隊合作時非常有用,團隊共享資料庫備份但不包括上傳圖片(這些圖片必須從程式庫中省略),因此當您將資料庫備份交給其他工程師時,如果啟用此外掛,他們將看到上傳圖片的替代圖片。此外掛提供了一個類似於 Uploads by Proxy 外掛的替代方案。
若您在另一台沒有上傳文件的環境上應用了生產資料庫備份,但您知道所有在資料庫中引用的圖片都存在於生產環境,您可以定義 MISSING_UPLOADED_IMAGE_REDIRECT_SERVER 常數,或透過篩選器 missing_uploaded_image_redirect_server 來使替代圖片服務失效,並將圖片請求重新導向至生產伺服器。
如果您使用舊的上傳文件系統,在多站網路中不適用,且 URL 中包含 /files/,在核心 #19235 中,可以看到解決方法。設定失敗的原因也可能是缺少在 Apache/Nginx 上啟用 WordPress 404 錯誤處理。如果您使用 Nginx 和 Varying Vagrant Vagrants 的預設配置,則需從 nginx-wp-common.conf 中刪除 png|jpg|jpeg|gif,以避免發生錯誤,該位置規則可處理所有靜態資源並直接提供文件的服務。
您可以通過篩選器 missing_uploaded_image_placeholder,添加支援您喜愛的替代圖片服務。
例如您可以在 functions.php 中添加以下代碼,或將其投入 mu-plugin 中:
<?php
/**
* 使用 Flickholdr 來作為替代場景服務
* @param null|string $url
* @param array $args 包含附加文件,寬度、高度和附件 ID
*/
function my_filter_missing_uploaded_image_placeholder( $url, $args ) {
$attachment = get_post( $args['attachment_id'] );
$tags = join( ' ', array(
$attachment->post_title,
$attachment->post_excerpt,
$attachment->post_content,
$attachment->_wp_attachment_image_alt
) );
$tags = strtolower( preg_replace( '#[^A-Za-z0-9]+#', ',', $tags ) );
$tags =
外掛標籤
開發者團隊
原文外掛簡介
Activate this plugin to redirect all requests for missing uploaded images on your blog to your favorite placeholder image service, such as placehold.it or placekitten.com. Note that although kittens are cute, the placehold.it service is actually more useful because the background and foreground color can remain consistant across all image sizes (e.g. full size vs thumbnail in a gallery), and so it is easier to see which images in a page are related to each other. (You can change the default placehold.it service to placekitten.com by defining MISSING_UPLOADED_IMAGE_PLACEHOLDER_BUILTIN to be placekitten_color or placekitten_grayscale, or supplying those same values via the missing_uploaded_image_placeholder_builtin filter).
This plugin is for use during development only. It is expected that this plugin will be activated on your local development environment (e.g. on Vagrant or XAMPP), or on your staging server. This plugin is especially useful when working on a team where you share around a database dump but not the uploaded images (which should always be omitted from the code repository), so if you give a database dump to another developer but don’t include the uploaded images, with this plugin enabled they will see a placeholder where the uploaded image appears. This plugin is an alternative approach to what is offered by the Uploads by Proxy plugin.
If you have applied the production database to another environment which lacks the uploaded files, but you know that all images referenced in the database do exist on production, you can define the MISSING_UPLOADED_IMAGE_REDIRECT_SERVER constant or filter missing_uploaded_image_redirect_server to short-circuit the placeholder service and redirect the image request to that server.
This plugin will not work if you are on a multisite network that uses the old system for referring to uploaded files, where the URL includes /files/ which is intercepted by a rewrite rule and passed directly to ms-files.php. See #19235. Similarly, make sure that missing uploaded files get served by the WordPress 404 handler, not Apache/Nginx. If you are using Nginx with the default Varying Vagrant Vagrants config, you’ll want to remove png|jpg|jpeg|gif from the following location rule in nginx-wp-common.conf (or remove it altogether):
# Handle all static assets by serving the file directly. Add directives
# to send expires headers and turn off 404 error logging.
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 24h;
log_not_found off;
}
You can add support for your own favorite placeholder services by filtering missing_uploaded_image_placeholder.
For example, you can add this to your functions.php or drop it into a mu-plugin:
post_title,
$attachment->post_excerpt,
$attachment->post_content,
$attachment->_wp_attachment_image_alt
) );
$tags = strtolower( preg_replace( '#[^A-Za-z0-9]+#', ',', $tags ) );
$tags = trim( $tags, ',' );
$url = sprintf( 'http://flickholdr.com/%d/%d/%s', $args['width'], $args['height'], $tags );
return $url;
}
add_filter( 'missing_uploaded_image_placeholder', 'my_filter_missing_uploaded_image_placeholder', 10, 2 );
Development of this plugin is done on GitHub. Pull requests welcome. Please see issues reported there before going to the plugin forum.
