wordpress精选来自外部网址的图片,无需下载
我想在wordpress上使用外部图片作为精选图片。
要么改变worpdress代码。 (添加一个预先准备好的图片metabox接受url并做一些修改,以便正确显示来自url的精选图片)
或修改插件WP远程缩略图,该缩略图从图像url中设置了精选图像。 它下载图片并在wordpress中创建缩略图并设置特色图片。
修改:*不从网址下载,只需使用url直接在博客上显示。 *从wordpress生成的网址中移除wp-content / uploads以显示精选图片(仅适用于外部网址)*不创建缩略图。
非常感谢你的阅读我知道有关于这个问题的很多问题,但如果我们解决这个问题,它可能会对很多ppl有用。
代码如下:
<?php
/*
Plugin Name: WP Remote Thumbnail
Plugin URI: http://magnigenie.com/wp-remote-thumbnail-set-external-images-featured-image/
Description: A small light weight plugin to set external/remote images as post thumbnail/featured image.
Version: 1.0
Author: Nirmal Kumar Ram
Author URI: http://magnigenie.com
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
?>
<?php
/**
* Initialize wprthumb on the post edit screen.
*/
function init_wprthumb() {
new wprthumb();
}
if ( is_admin() ) {
add_action( 'load-post.php', 'init_wprthumb' );
add_action( 'load-post-new.php', 'init_wprthumb' );
}
class wprthumb {
/**
* Hook into the appropriate actions when the wprthumb is constructed.
*/
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
add_action( 'save_post', array( $this, 'save' ) );
}
/**
* Adds the meta box container.
*/
public function add_meta_box( $post_type ) {
if ( post_type_supports( $post_type, 'thumbnail' )) {
add_meta_box(
'some_meta_box_name'
,'Remote Post Thumbnail'
,array( $this, 'render_meta_box_content' )
,$post_type
,'side'
,'default'
);
}
}
/**
* Save the meta when the post is saved.
*/
public function save( $post_id ) {
/*
* We need to verify this came from the our screen and with proper authorization,
* because save_post can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['wprthumb_nonce'] ) )
return $post_id;
$nonce = $_POST['wprthumb_nonce'];
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce, 'wprthumb' ) )
return $post_id;
// If this is an autosave, our form has not been submitted,
// so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user's permissions.
if ( 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) )
return $post_id;
} else {
if ( ! current_user_can( 'edit_post', $post_id ) )
return $post_id;
}
/* All good, its safe for us to save the data now. */
// Sanitize the user input.
$image = sanitize_text_field( $_POST['remote_thumb'] );
$upload_dir = wp_upload_dir();
//Get the remote image and save to uploads directory
$img_name = time().'_'.basename( $image );
$img = wp_remote_get( $image );
$img = wp_remote_retrieve_body( $img );
$fp = fopen( $upload_dir['path'].'/'.$img_name , 'w');
fwrite($fp, $img);
fclose($fp);
$wp_filetype = wp_check_filetype( $image , null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/.[^.]+$/', '', $image ),
'post_content' => '',
'post_status' => 'inherit'
);
//require for wp_generate_attachment_metadata which generates image related meta-data also creates thumbs
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_id = wp_insert_attachment( $attachment, $image, $post_id );
//Generate post thumbnail of different sizes.
$attach_data = wp_generate_attachment_metadata( $attach_id , $image );
wp_update_attachment_metadata( $attach_id, $attach_data );
//Set as featured image.
delete_post_meta( $post_id, '_thumbnail_id' );
add_post_meta( $post_id , '_thumbnail_id' , $attach_id, true);
}
/**
* Render Meta Box content.
*/
public function render_meta_box_content( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'wprthumb', 'wprthumb_nonce' );
// Display the form, using the current value.
echo '<label for="remote_thumb">';
_e( 'Enter remote image url', 'wprthumb' );
echo '</label> ';
echo '<input type="text" id="remote_thumb" name="remote_thumb" size="25" />';
}
}
我只是发现这个插件,似乎做你需要:https://wordpress.org/plugins/external-featured-image
我刚刚发现,您可以从远程URL插入图像,而无需先下载它们,然后重新上传它们。 这在默认情况下在Wordpress中工作,不需要安装任何插件。
就是这样:
无论你在哪里看到UPLOAD FILES,点击 - 你的文件浏览器会出现在你期望你必须选择本地文件的地方,但如果你只是将IMAGE URL粘贴到这个盒子中,按下OPEN或[ENTER],那么Wordpress会从远程URL。
点击选择文件从远程URL上传图像
将远程图像URL粘贴到您通常使用文件名的地方
我为此开发了一个插件 :
链接地址: http://www.djcxy.com/p/19465.html上一篇: wordpress featured image from external url without download