9

外部の写真をワードプレスのアイキャッチ画像として使いたいです。

worpdress コードを変更します。(URL を受け入れる Fetured 画像メタボックスを追加し、URL から注目の画像を正しく表示するためにいくつかの変更を行います)

または、プラグイン WP リモート サムネイルを変更して、画像の URL からおすすめの画像を設定します。写真をダウンロードし、ワードプレスでサムネイルを作成し、注目の画像を設定します。

変更点: * URL からダウンロードせず、URL を使用して直接ブログに表示します。* wordpress によって生成された URL から wp-content/uploads を削除して、おすすめの画像を表示します (外部 URL のみ)。 * サムネイルは作成されません。

この問題について多くの質問があることは承知していますが、この質問を解決すれば、多くの人にとって役立つ可能性があります。

ここにコード:

<?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" />';
    }
}
4

3 に答える 3

1

必要なことを実行すると思われるこのプラグインを見つけました: https ://wordpress.org/plugins/external-featured-image

于 2014-09-17T06:10:52.410 に答える
1

最初に画像をダウンロードしてから再アップロードしなくても、リモート URL から画像を挿入できることがわかりました。これは、プラグインをインストールする必要なく、Wordpress でデフォルトで機能します。

方法は次のとおりです。

UPLOAD FILES が表示されている場所で、それをクリックします。ローカル ファイルを選択する必要があると思われる場所にファイル ブラウザが表示されますが、このボックスに IMAGE URL を貼り付けて OPEN または [ENTER] を押すと、Wordpress は次の画像をインポートします。リモート URL。

[ファイルを選択] をクリックして、リモート URL から画像をアップロードします [ファイルを選択] をクリックして、リモート URL から画像をアップロードします

通常はファイル名がある場所にリモート画像の URL を貼り付けます 通常はファイル名がある場所にリモート画像の URL を貼り付けます

于 2014-06-12T16:48:43.743 に答える
0

そのためにプラグインの Featured Image from URL (FIFU)を使用できます。

于 2016-09-24T01:41:20.197 に答える