Add Featured Image from External URL

Home Forums Astral Meta Forum Community Web Design and Web Development Add Featured Image from External URL

This topic contains 1 reply, has 1 voice, and was last updated by Profile photo of AL2Meta AL2Meta 10 months, 2 weeks ago.

Topic Rating:
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1243
    #1260
    Profile photo of AL2Meta
    AL2Meta
    Moderator
    Points: 272

    Update

    I noticed that when you install the plugin WP Stateless, this function creates a “Warning: Missing argument 3” message above all the externally linked featured images on the front end of the website.

    I consulted the plugin authors of WP Stateless about it and one of them, Anton Korotkoff, a plugin contributor, told me that gmazzap’s code, particularly, the filter part: line 74 (refer to code above) $attr = apply_filters( ‘wp_get_attachment_image_attributes’, $attr, NULL ); is not correct. He said that this filter was stolen from WordPress Core, which is not bad, but it is used incorrectly. He said that the filter must get 3 arguments and not 2 ($attr, NULL). The third one should be $size and he referred me to /wp-includes/media.php line 919. So technically speaking, the code is missing one filter argument, which should be $size.

    Instead of $attr = apply_filters( ‘wp_get_attachment_image_attributes’, $attr, NULL );, it should look like this: $attr = apply_filters( ‘wp_get_attachment_image_attributes’, $attr, $size, NULL );

    Overall, the correct code should be this:

     Code: WordPress Function: Featured Image from External URL (select
    1.
    2.
    3.
    4.
    5.
    6.
    7.
    8.
    9.
    10.
    11.
    12.
    13.
    14.
    15.
    16.
    17.
    18.
    19.
    20.
    21.
    22.
    23.
    24.
    25.
    26.
    27.
    28.
    29.
    30.
    31.
    32.
    33.
    34.
    35.
    36.
    37.
    38.
    39.
    40.
    41.
    42.
    43.
    44.
    45.
    46.
    47.
    48.
    49.
    50.
    51.
    52.
    53.
    54.
    55.
    56.
    57.
    58.
    59.
    60.
    61.
    62.
    63.
    64.
    65.
    66.
    67.
    68.
    69.
    70.
    71.
    72.
    73.
    74.
    75.
    76.
    77.
    78.
    79.
    80.
    81.
    82.

    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_filter( 'admin_post_thumbnail_html', 'thumbnail_url_field' );

    add_action( 'save_post', 'thumbnail_url_field_save', 10, 2 );

    add_filter( 'post_thumbnail_html', 'thumbnail_external_replace', 10, PHP_INT_MAX );

    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;
    }

    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' );
            }
        }
    }

    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, $size, 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;
    }

    When I did what he said, the problem went away.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. You may login using: