add the post as canonical for attachment page wordpress

Here’s an (untested) example where we inject into the header tag on attachment’s pages, the canonical link of the attached post: add_action( ‘wp_head’, ‘wpse_attachment_parent_canonical’ ); function wpse_attachment_parent_canonical() { // Only target attachment’s pages if( ! is_attachment() ) return; $object = get_queried_object(); // Make sure we’re dealing with a WP_Post object if ( ! is_a( $object, … Read more

Use Gravatar as fallback image if no local image is not found

You can use the get_avatar_url filter (see the arguments passed here in the source code) to change the avatar url and then simply use get_avatar() with the user email in the theme as you’re used to. add_filter(‘get_avatar_url’, ‘wpse_avatar_or_gravatar’, 10, 3); function wpse_avatar_or_gravatar($url, $id_or_email, $args) { // was id passed via $id_or_email if ($id_or_email == intval($id_or_email)) … Read more

Get full URL of images in media library including http://

I’m sure some plugin or your theme modifies your attachment url. Try to find wp_get_attachment_url hook in your code or simply create your own with high priority this way: add_filter(‘wp_get_attachment_url’, function($url) { return preg_replace(“~^//(.+)$~”, “https://$1”, $url); }, 999); or better: add_filter(‘wp_get_attachment_url’, function($url) { return set_url_scheme($url, ‘https’); }, 999); But I recommend you to find the … Read more

Question about add_image_size()

First, let’s check the behavior of cropping. According to code reference: If false (default), images will be scaled, not cropped. If an array in the form of array( x_crop_position, y_crop_position ): x_crop_position accepts left, center, or right. y_crop_position accepts top, center, or bottom. Images will be cropped to the specified dimensions within the defined crop … Read more

A mystery 300px image size

Updated Answer After doing more testing, the 300px size only gets generated when I have the official WooCommerce Brands plugin activated. So 300px is the default WooCommerce image size when the standard sizes go undefined. See wc_get_image_size() in \includes\wc-core-functions.php. It seems that at some point WC must have filled in the blank of a missing … Read more

Escape post image attachments added to template

Running the output through escaping function should be just fine. You can either use wp_kses_post(), which by default allows the same html attributes that you would use in the post content (see in codex): echo wp_kses_post( wp_get_attachment_image( $image_id ) ); or if you want to be more precise and strict, you can pass an array … Read more

Display the featured image from the last post

This should work. I’ve modified your $args a little and surrounded your echoed code within the while(have_posts()) : the_post(); loop, then added the the_post_thumbnail() function and, finally, the wp_query_reset(); at the bottom: <?php $limit = 999; $counter = 0; $categories = get_categories(); foreach ( $categories as $category ): if ( $counter < $limit ) { … Read more

The the post id from image id

The featured image is stored as post meta with the key _thumbnail_id, so you could use a meta query to get posts with a specific thumbnail: $posts = new WP_Query( array( ‘meta_query’ => array( array( ‘key’ => ‘_thumbnail_id’, ‘value’ => $attachment_id, ), ), ) ); Where $attachment_id is the ID of the image.