WordPress Snippet: URL to Image Shortcode

There are so many times where we wish to show a URL as an image (screenshot). This WordPress Url to Image shortcode will help you embed image of the URL you wish.

Start with the below code WordPress snippet.

NOTE: To insert the snippet into your WordPress site, we recommend using the Code Snippets plugin instead of modifying your functions.php file.

// Snippet from NibbleGuru.com
function url_to_img_func($atts) {

    // Extract the parameters
    extract(shortcode_atts(array('url' => ''), $atts));

    // Make sure its an absolute URL not a relative one.
    if ($url != '' && substr( $url, 0, 4 ) === "http") {
        // Modify 500 to the default width you wish to have for your images
        // You can customize the value via CSS width property too.
        return '<img class="urltoimg" src="https://s.wordpress.com/mshots/v1/'.urlencode($url).'?w=500" />';
    }
}

// Hook the shortcode
add_shortcode('url_to_img', 'url_to_img_func');

To embed the image of a URL, use the following in your WordPress post or page contents, replacing https://www.nibbleguru.com/ with the URL of your choice:

[url_to_img url="https://www.nibbleguru.com/"]

If you wish to customize the width or look of the image you could also modify the CSS class, for example:

.urltoimg {
    background-color: powderblue;
}

 

Be the first to comment

Leave a Reply