There are plugins that could be used to achieve this.
To do it manually add the following code to your theme’s function.php
// Check if URL is image
function url_is_image( $url ) {
if ( ! filter_var( $url, FILTER_VALIDATE_URL ) ) {
return FALSE;
}
$ext = array( 'jpeg', 'jpg', 'gif', 'png' );
$info = (array) pathinfo( parse_url( $url, PHP_URL_PATH ) );
return isset( $info['extension'] )
&& in_array( strtolower( $info['extension'] ), $ext, TRUE );
}
// Add the URL textbox in Fratured image
add_filter( 'admin_post_thumbnail_html', 'thumbnail_url_field' );
function thumbnail_url_field( $html ) {
global $post;
$value = get_post_meta( $post->ID, '_thumbnail_ext_url', TRUE ) ? : "";
$nonce = wp_create_nonce( 'thumbnail_ext_url_' . $post->ID . get_current_blog_id() );
$html .= '<input type="hidden" name="thumbnail_ext_url_nonce" value="'
. esc_attr( $nonce ) . '">';
$html .= '<div><p>' . __('Or', 'txtdomain') . '</p>';
$html .= '<p>' . __( 'Enter the url for external image', 'txtdomain' ) . '</p>';
$html .= '<p><input type="url" name="thumbnail_ext_url" value="' . $value . '"></p>';
if ( ! empty($value) && url_is_image( $value ) ) {
$html .= '<p><img style="max-width:150px;height:auto;" src="'
. esc_url($value) . '"></p>';
$html .= '<p>' . __( 'Leave url blank to remove.', 'txtdomain' ) . '</p>';
}
$html .= '</div>';
return $html;
}
// Save the thumbnail with post
add_action( 'save_post', 'thumbnail_url_field_save', 10, 2 );
function thumbnail_url_field_save( $pid, $post ) {
$cap = $post->post_type === 'page' ? 'edit_page' : 'edit_post';
if (
! current_user_can( $cap, $pid )
|| ! post_type_supports( $post->post_type, 'thumbnail' )
|| defined( 'DOING_AUTOSAVE' )
) {
return;
}
$action = 'thumbnail_ext_url_' . $pid . get_current_blog_id();
$nonce = filter_input( INPUT_POST, 'thumbnail_ext_url_nonce', FILTER_SANITIZE_STRING );
$url = filter_input( INPUT_POST, 'thumbnail_ext_url', FILTER_VALIDATE_URL );
if (
empty( $nonce )
|| ! wp_verify_nonce( $nonce, $action )
|| ( ! empty( $url ) && ! url_is_image( $url ) )
) {
return;
}
if ( ! empty( $url ) ) {
update_post_meta( $pid, '_thumbnail_ext_url', esc_url($url) );
if ( ! get_post_meta( $pid, '_thumbnail_id', TRUE ) ) {
update_post_meta( $pid, '_thumbnail_id', 'by_url' );
}
} elseif ( get_post_meta( $pid, '_thumbnail_ext_url', TRUE ) ) {
delete_post_meta( $pid, '_thumbnail_ext_url' );
if ( get_post_meta( $pid, '_thumbnail_id', TRUE ) === 'by_url' ) {
delete_post_meta( $pid, '_thumbnail_id' );
}
}
}
add_filter( 'post_thumbnail_html', 'thumbnail_external_replace', 10, PHP_INT_MAX );
function thumbnail_external_replace( $html, $post_id ) {
$url = get_post_meta( $post_id, '_thumbnail_ext_url', TRUE );
if ( empty( $url ) || ! url_is_image( $url ) ) {
return $html;
}
$alt = get_post_field( 'post_title', $post_id ) . ' ' . __( 'thumbnail', 'txtdomain' );
$attr = array( 'alt' => $alt );
$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, NULL );
$attr = array_map( 'esc_attr', $attr );
$html = sprintf( '<img src="%s"', esc_url($url) );
foreach ( $attr as $name => $value ) {
$html .= " $name=" . '"' . $value . '"';
}
$html .= ' />';
return $html;
}
Refer this answer for more details – https://wordpress.stackexchange.com/a/158568/110516