How to add a media with PHP

If I’m understanding correctly, each post has a filemaker, and each filemaker has only one photo? The structure is kind of unclear. Anyways, one way is top use media_sideload_image like below. However, media_sideload_image WON’T work with local files (a path on your filesystem), so you need to change your $file[‘url’] to be a valid URL … Read more

How to display images in related posts?

That is simple: <?php $categories = get_the_category($post->ID); if ($categories) { $category_ids = array(); foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id; $args=array( ‘category__in’ => $category_ids, ‘post__not_in’ => array($post->ID), ‘showposts’=> 5, // Number of related posts that will be shown. ‘caller_get_posts’=> 1 ); $my_query = new wp_query($args); if( $my_query->have_posts() ) { echo ‘<h3>Related Posts</h3>’; echo ‘<ul>’; while ($my_query->have_posts()) … Read more

get attributes/part of the gallery shortcode

The task It’s not as easy, as it looks at a first glance. The main problem is, that you can define your own shortcode and simply override the default. Actually that’s what some themes are doing (blame you ThemeForest authors!). In this case, simply hooking into post_gallery won’t work in every case. Drawbacks for post_gallery … Read more

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