why when I try to insert an image attachment along with a post does wp_get_attachment_url give me a very wrong file path?

It turns out I was barking up the wrong (or at least a slightly different) tree by using wp_insert_attachment. media_sideload_image managed to pull attachments from other blogs on the same multisite install, copy them to the aggregating blog’s uploads directory, and generate thumbnails, while wp_insert_attachment was doing what it was supposed to, which just happened … Read more

Delete all thumbnails for a post

Using wp_delete_attachment( $attachmentid, true ) is only thing you need. Passing true as second argument, ($force_delete see codex ) it: remove the thumbnail association with any post delete any taxonomy associated to attachment and of course remove all the files, also the autogenerated ones So you do not need to also use delete_post_thumbnail().

get_the_post_thumbnail() returning empty string on custom post type

If nothing works, give this a try: $img_attribs = wp_get_attachment_image_src( get_post_thumbnail_id(), ‘thumbnail’ ); // returns an array if( $img_attribs ) { ?> <img src=”https://wordpress.stackexchange.com/questions/115607/<?php echo $img_attribs[0]; ?>” width=”<?php echo $img_attribs[1]; ?>” height=”<?php echo $img_attribs[2]; ?>”> <?php } ?>

Display height and width properties with the_post_thumbnail() or related function

You can collect image attributes using “wp_get_attachment_metadata“, see below example as starting point function mytheme_post_thumbnail( $size=”post-thumbnail”, $attr=””, $post_id = null ) { if ( has_post_thumbnail( $post_id ) ) { $meta = wp_get_attachment_metadata( get_post_thumbnail_id( $post_id ) ); $args[‘width’] = $meta[‘sizes’][$size][‘width’]; $args[‘height’] = $meta[‘sizes’][$size][‘height’]; $args[‘alt’] = isset( $attr[‘alt’] ) ? $attr[‘alt’] : apply_filters( ‘post_title’, get_post( $post_id )->post_title … Read more

How do I get a random image from subset of images in media gallery?

You can use taxonomies with the media library, you just need to either add a built-in taxonomy, or register a new one. For example, adding the built-in post tag taxonomy to attachments: function wpd_attachment_taxonomy() { register_taxonomy_for_object_type( ‘post_tag’, ‘attachment’ ); } add_action( ‘init’, ‘wpd_attachment_taxonomy’ ); You can then query for images with a specific tag: $args … Read more

Changing playlist shortcode thumbnail sizes?

Demo Plugin – Fixed Size Here’s one suggestion as a demo plugin (PHP 5.4+): <?php /* Plugin Name: Custom Playlist Thumb Size */ namespace WPSE238646\PlaylistThumbSize; add_shortcode( ‘playlist’, function( $atts = [], $content=”” ) { add_filter( ‘wp_get_attachment_image_src’, __NAMESPACE__ . ‘\\src’ , 10, 3 ); $out = wp_playlist_shortcode( $atts, $content ); remove_filter( ‘wp_get_attachment_image_src’, __NAMESPACE__ . ‘\\src’ ); … Read more

Adding featured image via PHP

Use the set_post_thumbnail function. set_post_thumbnail( $post_ID, $thumbnail_id ); Require you use WordPress 3.1.0 or later. You need call this function after you have successfully created your post via wp_insert_post and have a valid $post_ID.

How do I add a featured image to a single post page?

In single.php, find <?php the_content(); ?> around line ~17. Just before that. paste following code. <?php if ( has_post_thumbnail() ):?> <div class=”featured-image-wrap”><?php the_post_thumbnail(); ?></div> <?php endif; ?> This will show featured image if assigned to the post. To change the size of image you can pass parameter to the function the_post_thumbnail(). See official documentation of … Read more

Find posts without featured image? [duplicate]

You can use this plugin https://wordpress.org/plugins/quick-featured-images/ In description it says “You can also see posts with no featured image at a glance.” But from code side, wordpress manages images, documents any media by creating an attachment post for holding the information about that media and it’s relation (if any) with other post/posts, which means you … Read more