How to get image ID to use inside wp_read_image_metadata()?

I would like to automatically set the alternative text to be the same as the title of a post when image is uploaded. The attachment’s image alternative text is stored in the post meta table under the _wp_attachment_image_alt meta key. In media_handle_upload() and media_handle_sideload() we have: $id = wp_insert_attachment($attachment, $file, $post_id); if ( !is_wp_error($id) ) … Read more

Auto crop images in WordPress

You can use add_image_size to create your custom size like this: add_image_size( ‘featured-works’, 220, 180, true ); then get it like this: wp_get_attachment_image_src($thumbnail_id,’featured-works’); take into account how the crop works here

how to add an image to the footer?

Sounds like several different pointers may be useful: Are you working on a theme that was custom developed for your website? If not, you should create a child theme so that anytime the parent theme receives an update, your changes won’t be overwritten. The image you’re trying to add should either be uploaded through WordPress’s … Read more

Get an image’s alt text in a shortcode using the image URL

Instead of using the locationimage attribute, why not use the attachment ID? This will allow you to dynamically grab the URL to the file and will make getting other meta data such as the alt text much easier. Below is a rewritten version of hplocationsfn(). Usage example: [hpLocationSquare id=’2299′ link=’http://example.com’] Note that the link attribute … Read more

Add attribute to caption shortcode from custom attachment field

Here’s a solution that leverages the shortcode_atts_{shortcode} filter, where caption is the shortcode in this case. This works by pulling the attachment id from $out[‘id’] then getting the value of the class via get_post_meta( $attachment_id, ‘img_class_field’, true ). With this approach, it is not necessary to modify the output of the caption shortcode in the … Read more

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