Hot answers tagged

43

Here are few things you could try to remove the responsive image support in 4.4: /** * Disable responsive image support (test!) */ // Clean the up the image from wp_get_attachment_image() add_filter( 'wp_get_attachment_image_attributes', function( $attr ) { if( isset( $attr['sizes'] ) ) unset( $attr['sizes'] ); if( isset( $attr['srcset'] ...


35

Yes, it's possible and pretty easy. This is the workflow I suggest: Put somewhere a UI to insert the URL of the featured image. Probably best choice is to use 'admin_post_thumbnail_html' filter hook Use 'save_post' action hook to save the URL (after security and validation routine) in a custom post meta Use 'post_thumbnail_html' filter hook to output ...


33

As of WP 3.7 there is a much better way via the ajax_query_attachments_args filter, as provided in the documentation: add_filter( 'ajax_query_attachments_args', 'show_current_user_attachments' ); function show_current_user_attachments( $query ) { $user_id = get_current_user_id(); if ( $user_id ) { $query['author'] = $user_id; } ...


32

If you have the attachment id, you can use get_the_title() $attachment_title = get_the_title($attach_id) http://codex.wordpress.org/Function_Reference/get_the_title


31

Here's an improvement that uses the settings as you tried to do: add_image_size('medium', get_option( 'medium_size_w' ), get_option( 'medium_size_h' ), true );


26

Good news for WordPress versions 4.2+ Since version 4.2 the handy get_avatar_url() function, introduced as a feature request in ticket #21195 few years ago, now ships with the core: /** * Retrieve the avatar URL. * * @since 4.2.0 * * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash, * ...


26

There are four valid sizes built in to the WordPress core. the_post_thumbnail('thumbnail'); // Thumbnail (default 150px x 150px max) the_post_thumbnail('medium'); // Medium resolution (default 300px x 300px max) the_post_thumbnail('medium_large'); // Medium Large resolution (default 768px x 0(means automatic height by ratio) max) since WP version 4....


25

The answer above seems comprehensive, but I just wrote a wrapper function and moved on. Here it is if you need it (put this in functions.php): function get_avatar_url($get_avatar){ preg_match("/src='(.*?)'/i", $get_avatar, $matches); return $matches[1]; } and then use it wherver you need it in the template files like this: <img src="<? echo ...


25

When an image is uploaded in WordPress, it is saved in its original size, and also as some resized copies in different sizes. In this way it becomes easy to use different sizes, of the same image, for different purposes. By default, WordPress creates 3 copies in 3 different sizes: 'thumb' 'medium' 'large' The size in pixels for these 3 sizes can be set in ...


25

Your problem is that you are not providing the correct attachment's ID to get_post_meta() and get_the_title() functions. This is your code to get the alt of the image: $image_alt = get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true); And it is correct, but $attachment->ID is not defined in your code, so, the function does not return ...


24

you can write a php script, or make your own plugin of this code here, i used it in one of my projects where i had to import a large number of images. first, get the image, and store it in your upload-directory: $uploaddir = wp_upload_dir(); $uploadfile = $uploaddir['path'] . '/' . $filename; $contents= file_get_contents('http://mydomain.com/folder/image....


22

If you want display an image that is inserted into your content (a hotlinked image, for example), you must use a function like this (source): add in functions.php: function catch_that_image() { global $post, $posts; $first_img = ''; ob_start(); ob_end_clean(); $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->...


22

The add_image_size( $name, $width, $height, $crop ) function is graceful enough to handle multiple calls using the same $name. It simply overwrites the existing value: $_wp_additional_image_sizes[$name] = array( 'width' => absint( $width ), 'height' => absint( $height ), 'crop' => (bool) $crop ); So that means that all you need to do to override ...


21

Philipp, anything is possible if you set your mind to it. You can solve your issue by extending the WordPress image editor class. Note I'm using WordPress 3.7 - I haven't checked any of the below code in earlier versions and in the latest 3.8 release. Image Editor basics WordPress has two built in classes that handle image manipulation: ...


20

You can use the native Wordpress image_resize function to scale up images. Wordpress provides a hook called "image_resize_dimensions" which you can use to overwrite the default cropping settings. Here is a modified function which will support scaling up: function image_crop_dimensions($default, $orig_w, $orig_h, $new_w, $new_h, $crop){ if ( !$crop ) ...


19

function wp_get_attachment( $attachment_id ) { $attachment = get_post( $attachment_id ); return array( 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), 'caption' => $attachment->post_excerpt, 'description' => $attachment->post_content, 'href' => get_permalink( $attachment->ID ), 'src' =&...


18

it might not be the exact answer you're after but i think i just found a pretty good workaround. Iv'e taken the following code from the twenty-eleven theme CSS (who's neatly responsive imho): /* Images */ .entry-content img, .comment-content img, .widget img { max-width: 97.5%; /* Fluid images for posts, comments, and widgets */ } img[class*="align"], ...


17

wp_get_attachment_url() will only return url to original attachment file, this function only accepts attachment id as parameter. Use wp_get_attachment_image_src() or wp_get_attachment_image() instead.


17

There is an easier solution now: function my_gallery_default_type_set_link( $settings ) { $settings['galleryDefaults']['link'] = 'file'; return $settings; } add_filter( 'media_view_settings', 'my_gallery_default_type_set_link'); They added a filter to customize this default value (and other values related to the new media upload popup) in WP 4.0 (...


16

Came here as this post is among the top hits on the search engine when looking for WordPress image alt and title. Being rather surprised that none of the answers seem to provide a simple solution matching the question's title I'll drop what I came up with in the end hoping it helps future readers. // An attachment/image ID is all that's needed to retrieve ...


16

To remove the medium_large image size you can try to remove it with the intermediate_image_sizes filter: add_filter( 'intermediate_image_sizes', function( $sizes ) { return array_filter( $sizes, function( $val ) { return 'medium_large' !== $val; // Filter out 'medium_large' } ); } ); Not sure if you're trying to remove all the ...


16

Ok, I've been playing with this for a little bit and have managed to change the output of the Gallery block, with the following caveats: The preview does not match the output. I think this is possible but appears to be a bit more involved. Certain classes and markup are required in the output for the block to be able to parse the content and keep it ...


15

If you only need the names of all image sizes, you can use get_intermediate_image_sizes: <pre> <?php print_r(get_intermediate_image_sizes()); ?> </pre>


15

There is however another way: get_option( 'thumbnail_size_w' ); get_option( 'thumbnail_size_h' ); Note: thumbnail is one of the sizes set inside $_wp_additional_image_sizes. So the call to get_option() will work for every other image size too.


15

I figured it out! In my php.ini file I had to uncomment extension=php_gd2.dll, then it started working after I rebuilt the images I uploaded :) Thanks, Josh


15

The only time setting the quality really matters is right before the image is saved or streamed (for the editor). Both of these have the "image_editor_save_pre" filter there, passing it the instance of the image editor. So you can use that to modify the image in any way you like, including setting the quality. So, something like this should do the job ...


14

WordPress core doesn't have a native method for getting intermediate image sizes (i.e. width and height), but the following helper function will get all registered image sizes along with their dimensions: /** * Get all the registered image sizes along with their dimensions * * @global array $_wp_additional_image_sizes * * @link http://core.trac....


14

I have just built a plugin that does this. It is not perfect but it do its job. You can find it in my Github: https://github.com/pontusab/wp-ftp-media-library So you need to change some things within the file on the row 28: /** * Change this to match your server * You only need to change the those with (*) * If marked with (-) its optional ...


14

To built on Max Yudin's answer you should use the intermediate_image_sizes_advanced filter, and not image_size_names_choose. Add to functions.php function add_image_insert_override($sizes){ unset( $sizes['thumbnail']); unset( $sizes['medium']); unset( $sizes['large']); return $sizes; } add_filter('intermediate_image_sizes_advanced', '...


14

Problem solved - the image width was being limited by $content_width set to 640 in the functions.php file included with Bones WP theme. Hopefully this information will help anyone else struggling with similar problems.


Only top voted, non community-wiki answers of a minimum length are eligible