How to get featured image’s width and use elsewhere in template?

Try the following. First, add this piece of code to the template: <?php $image_data = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), “thumbnail” ); ?> $image_data will now return an array containing the url, width, and height (function reference). To get the width, you might do this: <?php $image_width = $image_data[1]; ?> In your specific example, after adding … Read more

How do you get the post thumbnail size?

When an image size is added either by WordPress(add_image_size), or by a plugin or your own custom code it gets added into the $_wp_additional_image_sizes global, i can’t find a similar function for pulling data from that global, but you could certainly just look inside the global to determine what width and height a registered image … Read more

How do you remove hard coded thumbnail image dimensions?

Related: Filter to remove image dimension attributes? There’s a filter on post_thumbnail_html which receives as its argument the full html element representing the post thumbnail image before it’s echoed to the page. You can filter out the dimensions with a bit of regex: add_filter( ‘post_thumbnail_html’, ‘remove_thumbnail_dimensions’, 10, 3 ); function remove_thumbnail_dimensions( $html, $post_id, $post_image_id ) … Read more

How to set featured image to custom post from outside programmatically

Can’t this simply be done with media_sideload_image() ? Seems pretty simple. Only catch is if you aren’t on admin area, you must include some libraries from within WordPress includes: // only need these if performing outside of admin environment require_once(ABSPATH . ‘wp-admin/includes/media.php’); require_once(ABSPATH . ‘wp-admin/includes/file.php’); require_once(ABSPATH . ‘wp-admin/includes/image.php’); // example image $image=”http://example.com/logo.png”; // magic sideload … Read more