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

Set media metadata (i.e. “dimensions” field) on SVG file after extracting it with a filter

The wp_update_attachment_metadata filter fires every time you upload an image through media library (specifically every time wp_update_attachment_metadata function is being called). So when an svg file is uploaded we check if the file has the desired metadata, the width and height. If not, we generate the metadata as you did in your wp_get_attachment_image_src filter. Now … Read more

Edit image preview is not displayed

By default, wp-config.php might not have a closing tag ?> and this is acceptable and by design. The issue may arise from whitespace in some other .php file. In my case, I had whitespace after the closing tag of my functions.php file for my child theme. Once I removed that whitespace, the images are showing … Read more

Upload images from custom plugin using the media modal

Ah! The classic issue anyone that’s cared about their users’ experience has come to face. As per my experience, wp.media is the way to go. This is not my code, but it gets the job done. I’ve used it plenty. I’ll explain what it does, piece by piece: // Source: https://vedmant.com/using-wordpress-media-library-in-widgets-options/ jQuery(document).ready(function ($) { $(document).on(“click”, … Read more

Advanced custom fields – taxonomy terms images [closed]

OK had a go at this myself, I didn’t realise ACF was able to add fields to taxonomies which is really handy so I wanted to figure it out too. <?php $libargs=array( ‘hide_empty’ => 0, ‘parent’ => 0, ‘taxonomy’ => ‘library_categories’); $libcats=get_categories($libargs); foreach($libcats as $lc){ $termlink = get_term_link( $lc->slug, ‘library_categories’ ); ?> <a class=”single-library-cat” href=”https://wordpress.stackexchange.com/questions/71287/<?php … Read more

unable to upload image locally

You need to change the file permissions on your wp-content folder (and containing folders) to 755. On your setup you can do this with: find . -type d -exec sudo chmod 755 {} \; Please note that using chmod -R 755 will mark both directories and files as 755.

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